 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 y+ Q1 T9 [7 c$ ?* [基本参数:
; F, u y g) B1 l; m) F9 ~+ ejta | thread | managed | custom.Class$ |+ V$ N: g! y! w; P
主要是配合
- v; `' t( D0 Y/ @3 q+ ^2 N: `3 t$ NSessionFactory.getCurrentSession()
) |9 ~/ `7 d/ K9 U8 z' v' U来用的.
u. {. c0 e6 y' K d+ h2 F- X( a! m# U9 _Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
! r3 }+ o( ?4 w) _% L0 J0 y3 p' DWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, [3 |1 N' l8 A3 [" J# D u
context.BeginRequest += OpenSession;8 q6 W1 ]+ J6 ~4 @$ [4 G: T
.Bind(HttpContext.Current, SessionFactory.OpenSession())
* ~% U: I9 [! f' F2 F2 z; f
/ E5 ~2 P7 g8 i. s4 ]//do transaction I Y" o5 u Q: F7 x6 O) U
getCurrentSession().Submit();
, ^" d+ k* a5 Q- \+ H" }getCurrentSession().RollBack();, n2 u" d4 m T6 s$ L
...# i5 {) @5 W! X5 K$ O9 O5 r7 u9 v
Unbind(HttpContext.Current, SessionFactory)* ^* ]& ?5 ~9 t7 N* z/ M3 _
; h3 T% n' U) U: V) S1 a Bcontext.EndRequest += CloseSession; h( @+ o8 E% [# e
Q! u! X5 ]! l1 c6 j) O
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|