 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' c5 M! [7 s2 _9 c" `' m
基本参数:
1 S1 d% | @* p& u/ r/ `% o: vjta | thread | managed | custom.Class
- m: s9 e$ B e) [: T主要是配合, s$ ?* b% v; l5 [7 T& F
SessionFactory.getCurrentSession()3 y# ~, V! H6 S. ^0 |" Y0 N8 d( ^
来用的.) ^$ X! t. B2 ^' s4 I% b
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
' y6 i; J. K; y% Z! I9 p# ^& yWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 e- U6 u) M9 Z5 Gcontext.BeginRequest += OpenSession;1 T$ w" [+ H, P5 ?# B5 D
.Bind(HttpContext.Current, SessionFactory.OpenSession()): M; t) A3 ] B0 D) N
! a/ F& H( a8 z" R- E' r. n//do transaction
3 x- j( f: ?$ b6 Q; LgetCurrentSession().Submit();
0 |5 n: a$ M) `2 IgetCurrentSession().RollBack();6 t5 K* m+ w- g) U3 c6 D5 Q4 M% O+ _
...
2 l7 e8 e/ Z( j% |) w" s2 bUnbind(HttpContext.Current, SessionFactory)% @- T* L! p4 N! O) m
- d T/ P' X9 m
context.EndRequest += CloseSession;( X E- t, ^. y% S* R) Q. e2 L
+ A2 h4 O e" r. Y" e% Y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|