 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
i( [: l* A. L4 K2 \5 c基本参数:5 W! E/ C" g: m* r8 ?# R0 c9 ?2 y
jta | thread | managed | custom.Class
9 R/ Y2 Z) C/ h2 U主要是配合
( I2 f# J5 h/ F9 s1 bSessionFactory.getCurrentSession()5 [9 ?" [& {' ?
来用的.
, @, x; L; Q- F* UThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ w4 r8 {% Z1 n3 G; x* p1 _
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( F6 Z K6 W( C
context.BeginRequest += OpenSession;
3 F$ A' e. T( t1 F.Bind(HttpContext.Current, SessionFactory.OpenSession())
# A6 Q! |& H) n3 h& T+ b# U; I- @6 m0 ]8 a0 s" b' C9 {4 l; |
//do transaction- R6 n: q3 t3 y3 b# n- u0 G2 ^7 c
getCurrentSession().Submit();6 ?: F( Z$ j+ ?
getCurrentSession().RollBack();- u' N$ R- O' c
..., n4 T, |4 k, w% r
Unbind(HttpContext.Current, SessionFactory)
% U3 F, ~" t( x' O0 s' n5 s4 |
2 |% P/ {% [7 P: Lcontext.EndRequest += CloseSession;
6 D* {7 f) i: N2 \$ t
* L- k7 R$ [ k, s 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|