 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' l- b! Z- ^) @& s+ t. T) X5 T) n- {5 `
基本参数:% J1 s! X0 p. w5 I
jta | thread | managed | custom.Class- d$ F7 T3 G% D! f* s0 D
主要是配合$ W, G1 B8 a" y) y
SessionFactory.getCurrentSession()
( l2 r: j. H; D7 e% U: D$ U来用的.
! o; T9 K# h6 t8 z8 p: T8 u, m( r; `Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
/ u: H/ U4 M% a0 H% ^Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; V* \; L4 f ?! ~; ]0 V
context.BeginRequest += OpenSession;" s' @8 Y. [% S6 t9 T2 u
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 U4 V0 o9 W+ E; B- X1 d: r" \' |
j! k; X: L! k* s I# V3 I& M
//do transaction
$ @5 J% l r- w5 K+ [3 \. ~getCurrentSession().Submit();
1 L7 a( [0 B+ ~! h: X7 u1 UgetCurrentSession().RollBack();0 g: v+ K- o6 @0 W8 n
...
; f/ o# m! v) O7 W& Q& F& `Unbind(HttpContext.Current, SessionFactory)
; p5 f2 T) B5 D( |% c+ F- L; M4 K; V u
context.EndRequest += CloseSession;
3 f' u t! f$ j, u; c
3 r3 W$ M/ Z; {2 J) t0 D% U# I 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|