 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 l1 k" G7 Y, e0 F
基本参数:
/ a" N3 D$ N$ ljta | thread | managed | custom.Class
4 q4 G- Z% z, S: f" A: Q8 G主要是配合
* [# D# D4 J+ b2 Q" ~) _; nSessionFactory.getCurrentSession()9 u- r0 E, f! I) `% D/ E/ U
来用的.5 J' L) k$ `; @* t1 ]
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% q5 b Q5 ^4 f" [6 a
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% r; |! v- F% W4 a; ?' Y* U! U Z
context.BeginRequest += OpenSession;
8 f+ i, W) C0 \# O.Bind(HttpContext.Current, SessionFactory.OpenSession())' K' o; Y3 M- o' o
5 P- H3 ~+ _% b$ S; n
//do transaction) y" V0 L4 X; m/ _& O$ q5 s) c6 J
getCurrentSession().Submit();
; b* n+ C y2 [& E* bgetCurrentSession().RollBack();
# y' \' o1 e* H7 ]* V, s...: b3 O/ D* u$ C0 Q8 M7 K( Q/ n; c
Unbind(HttpContext.Current, SessionFactory)
! q, `% `1 Z) [5 v) T/ D: e2 ?2 J, c# X) @2 v9 @9 X
context.EndRequest += CloseSession;0 E5 `* W1 Z" [5 ^* {
- S7 E! b1 N% h1 W$ k$ W
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|