 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. y4 ]: q+ t, _: L& Y& l x( ~; ^3 d9 j1 @
基本参数:
1 P! }/ M) K& H5 w: S3 ^! _( njta | thread | managed | custom.Class
L% u; l$ V/ a2 {: \主要是配合+ X6 f, R1 {5 W+ Z2 E7 B
SessionFactory.getCurrentSession()) b; j T% B$ D0 g8 c" j3 J
来用的.2 M+ a; S8 j9 m/ O
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* l: T& B& L5 Y# K9 L: jWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
2 l. ]9 E z0 _) d' @" m6 ucontext.BeginRequest += OpenSession;
" Z3 _2 D0 V7 f# \3 {.Bind(HttpContext.Current, SessionFactory.OpenSession())$ U# S# F, ~% o! u( R
: V' [ S; q* V8 `
//do transaction
' R0 e0 ]( P7 q1 ~6 I* B9 h& hgetCurrentSession().Submit();8 c$ C. j- B e. w; b6 p
getCurrentSession().RollBack();
: B9 {8 C# S# F& G, K* ~8 N1 c...3 j7 D& F$ Y7 T
Unbind(HttpContext.Current, SessionFactory)
6 N& K0 o0 N @7 [4 b. M
8 H4 S1 y- x; h9 [' }% G& `context.EndRequest += CloseSession;
! Q. e# ~. D: q" d) L" A
5 f8 n% n' ~7 Z# v$ P6 B/ B) x8 e- ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|