 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what/ B( p! ^* F* {9 [: f$ l
基本参数:
: b( r' [: \" h' u# o4 R5 a: N( p6 yjta | thread | managed | custom.Class
( I8 a0 A# i% D' l9 L$ X主要是配合
4 {, }' c% ~& `# p4 Q+ @SessionFactory.getCurrentSession()
/ R2 Y8 k9 l3 D" G; N+ t! r来用的.
' ]" c" r6 h4 d8 b4 V! p% IThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; H& n) @1 z& |4 fWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' M8 b$ Q% F# T0 x
context.BeginRequest += OpenSession;
, c6 B" C0 b( ^: [8 K.Bind(HttpContext.Current, SessionFactory.OpenSession())0 m+ A% t) w+ S0 Q
C6 B( n# @ s; p; j
//do transaction
: T! ^7 i6 z' w2 rgetCurrentSession().Submit();3 h' x1 B; }- m, u+ }/ C* V
getCurrentSession().RollBack();4 M: U7 W) b8 v1 A8 I/ D8 G$ e
...
! U0 ]" I! f7 WUnbind(HttpContext.Current, SessionFactory)% q% d$ J- F8 Q& E! B) q$ n" P
, e& E9 y/ L" y5 B
context.EndRequest += CloseSession;! @$ X9 B/ ?, q* b- e
* p+ L2 P. A: |" p
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|