 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
K; V( \! Q1 X9 g基本参数:
. i8 J0 D% F" a8 Ijta | thread | managed | custom.Class
( O( {7 y- z0 ~. G主要是配合2 n1 k' X7 M& u$ |
SessionFactory.getCurrentSession()
* m& A* c, ?! C- W& C" u来用的.
6 i2 c, L8 _, W4 P5 iThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: b+ K1 p: [4 @2 v2 sWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ t# q0 B9 E7 l4 g' U
context.BeginRequest += OpenSession;
" a" ~8 ]# A" L# X9 S' D4 L.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ ~# V$ v) Z6 ]( C+ J( A' p B* @5 \3 X7 I
//do transaction
$ W/ L9 T, y: c+ F- S8 P9 T2 VgetCurrentSession().Submit();
, ^, f& W% W) i8 A9 X$ ugetCurrentSession().RollBack();
+ ]; X% {1 x+ z+ ~3 b# \- O...
0 [0 L5 m0 e% g6 ^1 g) MUnbind(HttpContext.Current, SessionFactory)- Z- u# I4 L- w1 C
6 R$ W5 k; @$ ^- ~$ s2 w; A) d
context.EndRequest += CloseSession;3 S" z6 C. m1 }2 P3 T, u
" |& _3 M- L% k; S: ^* A. C" B 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|