 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! s0 P1 s$ c1 N基本参数:
) L' u) S; a. t! \$ G, _7 K. Rjta | thread | managed | custom.Class' t7 o4 w/ B, y" s# G5 H
主要是配合 z9 Q2 G# k+ Z5 ]4 I
SessionFactory.getCurrentSession()9 H7 T/ x' [8 z3 h* f4 q a$ F
来用的.
s: R/ z5 \" [/ KThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: _1 h3 F+ w0 p5 m& Z% S' UWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* ^3 V+ ]5 a) P. m; G7 S, W7 S2 Y
context.BeginRequest += OpenSession;$ z4 I5 {: t6 I9 S$ s
.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 C0 Q0 V" n; M: O$ B$ m8 z R: D3 ^. w7 d3 U
//do transaction9 b& x- \: b! o' U1 l3 n+ Z
getCurrentSession().Submit();
; G% W3 u- _2 h/ j% egetCurrentSession().RollBack();2 Y5 \9 `8 ? k' C: ^% C* R
...
4 Y" F* G4 u- Z! b8 v, i& pUnbind(HttpContext.Current, SessionFactory); ~+ u j' U( p, T) u! {
& |) [3 P# e5 b0 A8 S. Z! a
context.EndRequest += CloseSession;/ F1 a9 L2 { {+ I' ?1 t
4 \! i) ^' X! A( K& T1 K4 h 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|