 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. i$ C! V! W( r, O6 y4 \/ U
基本参数:
8 h, n, R8 ^' i5 k; d7 _jta | thread | managed | custom.Class* j4 ^3 B$ q* R5 B! H% w4 W
主要是配合
5 f, y' m0 d, @9 `# BSessionFactory.getCurrentSession()
5 g% [3 D' y' X" N" a7 R+ V来用的.3 K2 U) n, i) U B; s; y* o
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 W$ n& F, X1 {8 O" p5 `Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 U( z7 Y$ M. L9 bcontext.BeginRequest += OpenSession;6 L! X. g7 B; ]& a4 K2 m/ n
.Bind(HttpContext.Current, SessionFactory.OpenSession())/ k2 {" c/ L* u, y3 @& X0 @1 a
- a0 S, b- L( ]0 _# X7 Z/ l6 v//do transaction7 v& h# u; K0 M; C
getCurrentSession().Submit();8 V& ^& b; R2 J3 V
getCurrentSession().RollBack();/ J' h2 t& C- O' f- E* c
...
8 m% q7 u O c% B% n" ]Unbind(HttpContext.Current, SessionFactory)# c2 ^- a( f6 i) Y3 _7 F6 Z# ~; ]
+ e2 ?( D' t2 T: x
context.EndRequest += CloseSession;! X6 d9 u" W! T4 a! P+ o6 p; U0 R
. r8 }( `: n3 x 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|