 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
$ w. j$ a$ f( Q) B6 o' p% m! ]% \基本参数:
, D+ X. w' T" F, |. ?/ Qjta | thread | managed | custom.Class5 b4 M7 Y3 T( k: n* n7 U, O. f
主要是配合2 X+ j+ |; |2 |1 i& G2 ^
SessionFactory.getCurrentSession(). p. y$ n& t2 a9 R
来用的.
* F% L/ [) x0 M# QThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 _. _ y% s, X$ g1 O! \
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 f& {3 w! s% t. |9 G
context.BeginRequest += OpenSession;
' y6 k- ~1 B1 |/ b, g. p0 O. k.Bind(HttpContext.Current, SessionFactory.OpenSession())
! P9 c# f: o% h) g9 z6 k
& p( h9 @9 r" @* a& n//do transaction
+ v' ~* Z2 G4 R; B. B& qgetCurrentSession().Submit();
: k4 Z$ u: \9 t, c4 H4 WgetCurrentSession().RollBack();0 Z0 u& u2 F, y
...9 S/ H' o; y; ?) n1 r" _- M
Unbind(HttpContext.Current, SessionFactory)
' g' ^! N2 S0 N8 r1 R1 ~( f9 R8 ?$ Z* U# E0 p; k) @: T9 ?+ _
context.EndRequest += CloseSession;
F& |" P6 k3 \4 }7 B6 N
! ?$ X: t. P+ t7 |1 b 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|