 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 o/ I/ @2 E; ~* V) h/ T
基本参数:
8 `* @' S% Q" Q. R7 E( {# _jta | thread | managed | custom.Class
% S& Y% ^9 f& S, A! s! C/ d主要是配合3 D0 o% B4 Y2 n# U: _+ f4 G1 v) \
SessionFactory.getCurrentSession()$ Y# B) z# g9 S ]# m1 d8 c' {4 w: g# O
来用的.* z! L- G0 F, ~; R' K# ?; i
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
# j) e% ^$ ^8 fWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# ]' L/ I' }6 R1 c! s4 |! icontext.BeginRequest += OpenSession;
' v9 `7 `; t* o4 X1 U, z! q! g.Bind(HttpContext.Current, SessionFactory.OpenSession()). p; w* a+ ]# L* ~0 u" [5 f) x' i3 C
8 Q: X; M" n; h8 V3 Y//do transaction- k- I5 \ f; n, d$ ]" t
getCurrentSession().Submit();) M1 S, [8 R k+ i
getCurrentSession().RollBack();
( _1 r& f' p$ v& O...
1 M; N9 U9 {- C, {Unbind(HttpContext.Current, SessionFactory)6 w9 p6 [* X( I5 Z: g6 [
5 B( ~) u( w% x+ W4 v8 l1 a, ]: S
context.EndRequest += CloseSession;
O& u9 _, z2 k
0 d1 h5 ^: B7 z6 x9 c. [! d- z 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|