 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& g- ~1 I s: P$ M基本参数:& s5 W' c! Z6 m+ l; I
jta | thread | managed | custom.Class }- `% z5 E+ H4 g% j& x
主要是配合
8 {1 L- w0 j2 r8 Y) ESessionFactory.getCurrentSession()6 Y Z0 P. t2 V5 f5 ]
来用的. c& n6 F5 w6 H2 ?* O6 K1 H, K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 X) w. }" L5 s( `4 z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:# h, s- W4 Z, i
context.BeginRequest += OpenSession;& o# x v: t: ]. i
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 p4 \" w% y" F5 d, u. L! g
9 w4 m2 x! a% _' \: j1 s" r
//do transaction
, ^6 J. [. x% V# K* K! @getCurrentSession().Submit();% V; [- i. y# \! O$ G6 a
getCurrentSession().RollBack();
# x$ C# ^$ M2 u& `4 }2 g, _' N# L...
& c6 @- b# [6 b. ^Unbind(HttpContext.Current, SessionFactory)
: X4 v$ [6 o- B/ ]9 z/ p
k+ r* p5 Z& \0 {3 U6 lcontext.EndRequest += CloseSession;
* [, _1 x: W% S% \, Z: V# h" t" x6 @% I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|