 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what B( y7 y; S" s- [9 r8 ^/ `2 N
基本参数:8 ^% Z' Z6 K$ v! ?4 V
jta | thread | managed | custom.Class
: ? f4 D! H7 |0 _9 }主要是配合4 n, n' x4 j% i$ q5 ?
SessionFactory.getCurrentSession()! X2 |1 i; b& `6 K8 L: e
来用的.8 w- s7 S5 w! U: n; _8 V* d
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) K [# R- I: O7 |& C( X
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:0 k* A0 ]+ x) g* u
context.BeginRequest += OpenSession;
, K6 B0 I; P: a) O.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 E. `, Y& B' ^* S) z1 r. A+ P
//do transaction- r" ?6 p; O' O m2 `1 e# W
getCurrentSession().Submit();
7 C( |* q# O% B: R% q: vgetCurrentSession().RollBack();+ h! v. v2 J, |4 @; y: v9 V
...3 j, K2 H3 s. z$ p+ ?
Unbind(HttpContext.Current, SessionFactory)
( v5 a! ?7 e6 B5 {) k0 N) X* h D. Y4 F9 _9 b
context.EndRequest += CloseSession;
& i) X9 B$ |% |$ g3 v3 v% q) W. s& C
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|