 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) S" D2 S/ g$ H2 r ], ~
基本参数:
: a# d0 X4 s/ _* g; t# @/ xjta | thread | managed | custom.Class
% ?& r, P+ B! R* v: G" ~- \主要是配合
, g; l. b: j: Y, q/ g' ~$ m: N, vSessionFactory.getCurrentSession()9 v6 J3 ^6 a! b/ W/ x
来用的.
! Q* H/ x+ }( W9 Z0 [ e) aThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, z7 W0 x9 L+ y: u8 h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
Z% I( k( @/ Y5 c2 N& t. k7 dcontext.BeginRequest += OpenSession;/ ^$ O+ {1 F- ~ z" l0 [# @
.Bind(HttpContext.Current, SessionFactory.OpenSession())
0 @9 m+ ?. i" E7 `9 B! m) W* ]- ~ F; T6 n1 s
//do transaction4 }2 `8 ?. u+ P6 D& c0 O
getCurrentSession().Submit();# p( D: d3 I) e8 I [
getCurrentSession().RollBack();
, ~* {! _; k, { W) T0 C" I...
0 i2 ?6 ~: Q, z$ y5 [Unbind(HttpContext.Current, SessionFactory)5 g8 c3 s. H0 ?6 H
- H: f0 i% Q' L, t0 t6 z$ o
context.EndRequest += CloseSession;
2 ~6 e/ \; a. H% w+ \0 r. B6 h2 U: X0 N& R" K
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|