 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& r- i) E/ b. E2 I- J q+ j2 P" n' a# c基本参数:
9 K' }3 D/ {( K. l. v$ z& bjta | thread | managed | custom.Class
, u6 Y2 o: b+ I5 e2 ^" e, u主要是配合1 F: o1 K- W4 u" N/ B: |* t
SessionFactory.getCurrentSession()
- e# X. P* v0 W2 E. {来用的.
( w' w) m" t1 j% @' ZThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
[3 I5 W! _- w" a' ^2 r' xWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: R! B7 G( I; V) o2 ?, {( j7 V
context.BeginRequest += OpenSession;
8 j1 M' z7 P* c9 p! X1 M m.Bind(HttpContext.Current, SessionFactory.OpenSession())
E8 f, k; `, i/ V! Y# g$ K
5 C2 \* K* s/ |) R//do transaction2 x& z" R2 a- q% ?: _( Q+ s' w
getCurrentSession().Submit();, }% q' _1 P) W: G O
getCurrentSession().RollBack();; Z( ^5 u+ m* b E
...
7 x# F9 k: u0 I( BUnbind(HttpContext.Current, SessionFactory)
z q/ R9 n* P: X2 X
! |3 ^, Y6 J: e econtext.EndRequest += CloseSession;
! @; q3 r. M/ T3 |1 b
% o0 w- J9 s7 d7 y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|