 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 P) c% e3 Q, B( G+ O基本参数:
" n2 \8 d3 L+ ljta | thread | managed | custom.Class
' f# ^6 x* ?( D4 I主要是配合
& L/ [2 h" G6 t( y1 uSessionFactory.getCurrentSession()
* T3 \, k2 s! _来用的. J# w- H$ s+ \7 @# ]2 E
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: h% V% S! C `/ \
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:0 Q! Y* ]% E/ L) [; |1 e% A
context.BeginRequest += OpenSession;
/ G, m% i& ]7 X: a- q0 q! L, I5 ~.Bind(HttpContext.Current, SessionFactory.OpenSession())3 G6 m& B( E8 Q/ a7 K& o; @1 F
, H+ R8 y- k7 Q K; V9 ~//do transaction
O$ s6 c6 V9 ogetCurrentSession().Submit();/ }7 z; o% T/ H- u) f
getCurrentSession().RollBack();7 }+ q, G' H6 M2 O
...
! O' ?" O [* {2 l' x) AUnbind(HttpContext.Current, SessionFactory)* W0 C/ W6 K' F4 J7 h9 J$ G& M
: y0 D# H: g: z* C- H1 _ G
context.EndRequest += CloseSession;- G: g9 N) L! T
; \- }2 ?( o n$ s3 x* H* x( c' n 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|