 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
$ i2 y% W2 ?( O基本参数:4 E) z0 m; }' y# Q n
jta | thread | managed | custom.Class
! Y0 c& m, Y7 Y& D F* n Q主要是配合
7 i- n: A" c( q& ?7 j1 i& aSessionFactory.getCurrentSession()
# ?" t1 }1 H3 k" }来用的.; h* @8 I) K, W f0 i7 Q
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,4 k' r+ m. S0 [1 k0 R
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
0 Z! e0 ]" s( y1 T. V/ ]4 Jcontext.BeginRequest += OpenSession;' y" G3 n$ C/ N C+ M& D
.Bind(HttpContext.Current, SessionFactory.OpenSession())
+ r" t4 D8 J: I& Y
, u3 U8 T1 m1 B' R//do transaction
+ b+ G) E" G. D7 W$ Y" s- zgetCurrentSession().Submit();6 F7 [) z% \& G& J3 T l
getCurrentSession().RollBack();4 I7 C% V# h4 T2 ]2 N
...
1 Y0 w1 z% s; f" C6 TUnbind(HttpContext.Current, SessionFactory)
2 M9 I+ }4 l1 O5 o; n/ q# V
f5 G/ U" L* k6 N# {context.EndRequest += CloseSession;+ I( k2 `4 m% y0 j( b. p2 j' D
% w' e6 ~" \, ]; a+ x' C& B
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|