 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
' ~" P% }; N- ~& [$ }% O8 v2 ?0 ~% m基本参数:* U: R8 C& B! b* p* F3 x9 O, W
jta | thread | managed | custom.Class
0 ?- C Y. }$ s: ^8 l4 z" A主要是配合3 z& a; i+ i7 _) |
SessionFactory.getCurrentSession()
' @# I3 C# ?. A! T来用的. A) ?$ U9 ^" f g* C, I
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) B W7 C: d( R& f4 h) c5 g
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 T; i) P3 j, j& i4 z0 c1 v
context.BeginRequest += OpenSession;5 P; m/ M2 ]5 C1 ]7 p# D
.Bind(HttpContext.Current, SessionFactory.OpenSession())% |" s% M2 z3 d$ v$ V
5 E8 i0 u! f) m5 M0 G X
//do transaction
7 C* H, `* H; f. |8 p+ OgetCurrentSession().Submit();
/ S, H; x, ?/ I& F$ O6 ]% [0 v$ x2 HgetCurrentSession().RollBack();
, L8 |+ Y8 [' o* ?8 L...5 b2 a9 j) \7 p( @+ h0 f
Unbind(HttpContext.Current, SessionFactory)% l$ Q. t3 N$ K
9 F, Q/ n3 M$ W. s; b5 M4 r$ Lcontext.EndRequest += CloseSession;
8 c6 R# M) T5 U- |. e
$ M0 O9 ?' I3 u1 ?7 {2 ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|