 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; u$ P, T% Y) L; O# [2 F基本参数:( X; p) q7 q* ^9 D6 u( J
jta | thread | managed | custom.Class8 p2 Q2 \( q* I+ P
主要是配合' p- Q g" ? J# |, b: n
SessionFactory.getCurrentSession()
1 p! S( Y4 v9 G8 O. G, e来用的., N& l. X3 u# S f% X7 x
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,# c, n' ~2 o6 M5 r5 x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 ? o5 H5 T2 J
context.BeginRequest += OpenSession;; B" [8 D9 `/ @ R8 g$ b6 l
.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 Z* J: j( ^5 `& q8 ^9 u' ^4 V0 n$ K) v' F$ w; X9 q; G p. e
//do transaction
, K+ h" h. j# w# G/ Y8 RgetCurrentSession().Submit(); M( L+ G7 |. t; F9 W8 U/ k
getCurrentSession().RollBack();/ c. n4 p2 F! p- ?1 A$ s% N5 ^
...2 v# U! X/ f" R+ g$ O
Unbind(HttpContext.Current, SessionFactory)
* i1 k) U+ X% k* [! N: c: L' C5 T, H. m( l& r: W* s- Y
context.EndRequest += CloseSession;
6 h3 J8 V2 A5 p7 z$ [8 r2 O4 E
0 w& J8 {1 i* q1 Q8 C 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|