 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
) K% f+ S1 t# V$ h! m基本参数:, x; h; R/ r( D
jta | thread | managed | custom.Class
+ j. Z/ y; y, }: h- C! D主要是配合
% E; V3 f# j4 I# d. q% M rSessionFactory.getCurrentSession()3 d7 M1 i+ G; o: Z
来用的.
3 L5 Y! O2 C9 H9 Z: nThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, C( R: K2 t0 U- B& r
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- {+ g/ a7 D0 a. M( Ccontext.BeginRequest += OpenSession;! K4 N9 O# z6 Q5 m N; y6 }
.Bind(HttpContext.Current, SessionFactory.OpenSession())" C1 [$ Z, g0 f9 k
5 a" T* C0 K; X7 A$ B
//do transaction8 Y) e& T9 l/ U( A# E1 P
getCurrentSession().Submit();) F1 n) h9 `' e& K; b' h1 O3 A
getCurrentSession().RollBack();8 C: w9 G+ r$ o4 [$ z; C- i
...( ]8 P% @) U/ h2 x' H
Unbind(HttpContext.Current, SessionFactory)
2 S/ U+ r6 ^0 [# z7 O) V' L
) H' }8 `! r, @context.EndRequest += CloseSession;
# P& o& F1 x( [% S
/ v5 L/ C( j, o& N! C% A" `( M 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|