 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 G0 q& O- o; b& p; g( t! Y
基本参数:
; b2 ^( \# n' G1 k3 ujta | thread | managed | custom.Class
$ ]" e' T6 c+ n1 p9 }主要是配合
9 r. L5 j. K3 ]" Q( T+ D! a0 s5 GSessionFactory.getCurrentSession(). d1 p& N5 |4 u5 o, i" T- V
来用的.- i. L/ {$ k4 B' N( ^8 h; i, P
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,4 X- x0 A0 y; B7 L% j9 Z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:: z7 x" v0 x; t3 t/ p
context.BeginRequest += OpenSession;7 s7 l& \, @3 T0 Q. v
.Bind(HttpContext.Current, SessionFactory.OpenSession())) c1 s7 i3 `0 i# {" r+ a. L9 x
0 ~3 `' c& z! y
//do transaction% T* h1 w/ z7 q, r; `# s
getCurrentSession().Submit();; q/ G" W9 E5 ?
getCurrentSession().RollBack();
# q+ {8 w( @$ H/ O8 b5 g$ \2 g j...
) `5 v* p: i% s+ ?* V$ \# xUnbind(HttpContext.Current, SessionFactory)2 _% b; `6 I2 Y0 g) J% E- j* g
" p; J# l8 }0 b8 Y0 a. I, G1 ?context.EndRequest += CloseSession;
s! [) H% Z, ?2 k, b; k4 T, B
0 o, {" ^# T9 U2 {0 w8 ?6 N 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|