 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ P' A7 E9 B4 x6 N* V8 f基本参数:
$ k1 [) d3 B, v5 X: B0 Z6 V. c( `jta | thread | managed | custom.Class* M d! A8 C; \) V, G
主要是配合
- q) o! o4 D% e. hSessionFactory.getCurrentSession()0 z; t# G \5 c* N0 ~7 {9 X& S! X
来用的.
) k! e5 y e+ {9 `6 h0 w& S4 wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ b j; }) U6 a) T& p7 h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 _2 q& W6 z, }% Ycontext.BeginRequest += OpenSession;: _6 O- l% M6 t* H5 R% b+ G
.Bind(HttpContext.Current, SessionFactory.OpenSession())) N) s) f3 m/ R. V' \8 l- b
& J2 Z+ ~0 ^- g6 e
//do transaction
v" F1 X. g8 Z6 z# w8 ^getCurrentSession().Submit();! k6 M. ]5 e. L& B8 N
getCurrentSession().RollBack();% d8 t' V% m( ?7 d3 K' D
...
; N5 f8 F' Q5 gUnbind(HttpContext.Current, SessionFactory)5 b$ `6 L9 E% Q! w
1 n: S9 b) P7 e b: y0 H8 ucontext.EndRequest += CloseSession;
6 k+ }$ n. Q" x/ l* P8 h* v/ H( Q
- J* k: Y5 Y) Y3 @- W% J 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|