 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what+ {" Y g- u3 p4 p
基本参数: j6 Y F% I4 n5 S
jta | thread | managed | custom.Class* Q" X0 H+ G( p' Z; q5 n v
主要是配合
8 I+ e: d+ P; C" g( o7 w, xSessionFactory.getCurrentSession()
/ |7 k; M6 B; w& |; P1 d来用的.
" s7 W8 H3 @" U3 xThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% X- k( Z( W) i" B+ {6 C' n
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% j4 t o8 p! C! d
context.BeginRequest += OpenSession;
2 p4 p1 o' u: e: ?1 x0 ^.Bind(HttpContext.Current, SessionFactory.OpenSession())
) j! y; ]: k, p7 {3 n: A- ^- v& H7 H( {: ]2 t, b6 G
//do transaction; f% @- t6 u% a
getCurrentSession().Submit();# ^& q5 h+ a3 d: Q$ q
getCurrentSession().RollBack();: {3 p: f2 ~! t y
...
+ u0 H" Y1 B( R: v3 R' a, WUnbind(HttpContext.Current, SessionFactory)! u! w5 b9 X7 F' T/ l. {. }5 V6 h
6 Y8 k/ Z, r' |# Z7 y
context.EndRequest += CloseSession;1 m7 ~( Z- v+ D# K C( l
8 o+ t8 J: M% T1 |+ o% J: J
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|