 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. Z+ H0 o9 z- o5 g+ u5 Y
基本参数:6 m5 n9 Z0 G% `' {& c
jta | thread | managed | custom.Class1 v; ^, Q& H5 I8 e4 |
主要是配合
; w2 Y% Y. C9 C9 u4 \/ z* C8 NSessionFactory.getCurrentSession()
. V% p# {/ u2 R1 \( R G; y来用的.
) t! i& n+ A) a, ^% z2 p R: ~6 rThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 G; g. t& }- U( q' A+ A! q9 {
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& t- K! H# J4 W( f) q4 r0 K1 l
context.BeginRequest += OpenSession;
7 Q9 @1 z2 Y/ o; R' e1 f- _ ?- |.Bind(HttpContext.Current, SessionFactory.OpenSession()) z g4 h% k$ D% F
. S; \8 u0 k6 M3 s7 M, b( D, O, ~//do transaction
5 v' T2 S6 z1 {1 ngetCurrentSession().Submit();4 Z9 Z. \$ p: s
getCurrentSession().RollBack();
; M! _% z- M- o5 }% X...
3 j5 e" C' M, { AUnbind(HttpContext.Current, SessionFactory), [/ b% S, Z1 O* x
# U7 o0 J% C* v1 b' \4 r
context.EndRequest += CloseSession;
4 p8 k3 }. F+ A3 S2 o. v
9 |, }$ y: |4 z7 t6 i& _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|