 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
$ c$ N4 B* c) [6 J1 L4 f+ F% O基本参数:/ J/ ~4 ]6 D. A! w4 p1 i# ^( H0 r3 y
jta | thread | managed | custom.Class
2 g- t* N9 c, M& @( S g& s主要是配合/ Q6 _9 t& W M2 ^
SessionFactory.getCurrentSession()
- Y7 ?7 `8 x. P- M, `4 M6 Z- p: s来用的.
; M' S' V- |) {; q- m. g9 xThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 u, U* F* S, M! V- m* h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 q7 W8 H' w5 U* l0 ]
context.BeginRequest += OpenSession;
* ~* u, c! _5 b& G.Bind(HttpContext.Current, SessionFactory.OpenSession())
1 n' _4 a/ g# G4 f# K1 Z0 R+ S ?' I" Y( `- s1 a
//do transaction. V) P6 _- h4 p N6 V8 v
getCurrentSession().Submit();
$ h' s/ s1 I. _9 R7 d* SgetCurrentSession().RollBack(); h3 g& L9 S' U, @0 n1 M
..." ?/ n1 G6 n8 |) U& V( }$ Z/ D
Unbind(HttpContext.Current, SessionFactory)
- }; y A( M! o+ X# D; D$ k
7 p& K; o3 X$ V2 p2 H4 M9 }context.EndRequest += CloseSession;
, k# c9 z9 v" H% r4 b
2 Z% `5 u; v5 `4 H( C C; }$ \4 k( h 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|