 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what, y1 d! P) V- N9 s- n0 | O- ]' g* }
基本参数:( o# k7 F! t. a3 z
jta | thread | managed | custom.Class
3 J1 j/ w5 R! c h" B- L主要是配合) r2 N. G0 t0 Q; s* M% g$ y
SessionFactory.getCurrentSession()
7 O6 q2 w4 f' T" F来用的." B3 C% ]8 Q: K- s# Q) E
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- j, J3 W9 i' L( N w1 R; B
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 i1 b9 p4 C! a
context.BeginRequest += OpenSession;
" Q& K2 T: I' K.Bind(HttpContext.Current, SessionFactory.OpenSession())/ J1 R/ Q% ], p4 K
9 @, W+ t0 i4 {+ ^7 Q//do transaction' ^; M2 \- V- ^7 `) f6 a9 o" \
getCurrentSession().Submit();- Q' M- v z6 a5 c$ m/ N' s L0 E5 U
getCurrentSession().RollBack();
* n4 Q0 I) p# H, A! g...3 C R/ q5 t$ g1 C* x
Unbind(HttpContext.Current, SessionFactory) O% s# p' ~3 W
! ? O9 ~. k7 Y) V r
context.EndRequest += CloseSession;" }: M& I$ D& k" w2 k
6 d* k( ?, N+ t2 k8 u
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|