 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! @$ e, _4 N" l# o7 h: R# j3 W基本参数:, s2 i$ Z+ ~1 b
jta | thread | managed | custom.Class
* s, u- t" {* A主要是配合, |* D! `6 ]+ ~6 M, O* t7 h" l
SessionFactory.getCurrentSession()4 [1 U: ?% C" d4 n% u3 ~/ N# T
来用的.: E' d0 }% g; w$ x1 i9 U
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
+ A5 c' y- C1 TWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* ] h- _* B: G; f, @
context.BeginRequest += OpenSession;( `7 Y7 F% q7 j$ K
.Bind(HttpContext.Current, SessionFactory.OpenSession())
* O. l$ ~. V k" W6 z) ?
`. v6 I4 ?3 r6 l- t! E0 q% Z r//do transaction
5 n; w! T9 ?4 c0 `7 P. MgetCurrentSession().Submit();( b( i. u8 L; [6 Q! f \
getCurrentSession().RollBack();$ A' H3 d. w" P& G/ L) I/ ^
...
: i3 ^# j0 H/ h- P5 [: a5 W+ PUnbind(HttpContext.Current, SessionFactory)
4 \: _" r' L j( m
0 W7 I% E! U3 v8 e8 |context.EndRequest += CloseSession;, Z+ N# U: L0 G t! U6 U( D" P: P
: h( g1 t% }# ~8 R9 s( h0 k @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|