 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 ?$ f- _7 C! r1 Y5 E; B" h4 F8 T基本参数:
+ @) E5 T" ^7 h" K9 P, U& u3 O, T; Wjta | thread | managed | custom.Class' x6 W& u% O, n( [+ q6 U0 Q
主要是配合
" {) R" c8 l: n3 x6 OSessionFactory.getCurrentSession()4 C- C2 ?. a- j1 ], T: [! Q7 o/ ^
来用的.
+ n# I6 o' T5 S, bThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 g4 z3 x* p2 G( i7 ^8 `" K( j% eWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ g8 J* _' x! ^7 n
context.BeginRequest += OpenSession;
. m4 H$ q& [" R. Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
! j+ I8 e6 y+ q- N3 A- z6 k! b: j) {4 ~1 y
//do transaction+ ^8 J' b& {( R1 C/ H7 N$ j
getCurrentSession().Submit(); b7 m6 T) G+ m" g
getCurrentSession().RollBack();
+ e' A) a- F$ x, r h...0 o! _( E6 t2 f1 V, U$ R5 @: a# F
Unbind(HttpContext.Current, SessionFactory)& ^! l/ t( W3 t& z* N C& N. c
& c( Q/ @8 j" a" a
context.EndRequest += CloseSession;
3 w, i. `% m' @3 ^& E
* D% b1 x! A( F: ?4 t0 |5 f+ H 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|