 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
' z8 |8 s9 g1 v6 J& F; T0 e基本参数:$ s( M: f8 \6 p
jta | thread | managed | custom.Class. }% \) X: [7 L v4 s O* C
主要是配合
0 ^* J! j, X: a! n% I0 gSessionFactory.getCurrentSession()
/ O7 t" Z% m! v6 _. B' C2 z来用的.8 O0 d! I/ }* a, R0 E
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 O1 N" k* G" M6 A: l
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& H: T. M3 }$ j, k2 ^& n2 ucontext.BeginRequest += OpenSession;
+ S# t* |8 n! d.Bind(HttpContext.Current, SessionFactory.OpenSession())
. C2 G7 A. H6 C- u2 a% P4 m1 r/ ?2 I& Y( _ R
//do transaction
0 z( J8 {' r- n/ h* h5 Y7 ?getCurrentSession().Submit();. o8 W; x7 P3 z% x8 n0 e0 d
getCurrentSession().RollBack();
( ^9 e- W6 y- c3 q+ M! ~...
: j' B4 ^% H: Q- cUnbind(HttpContext.Current, SessionFactory)$ D1 k4 j3 h9 g4 B
4 w; }$ N; E6 Z- u. H: C
context.EndRequest += CloseSession;
( `: O5 I6 X+ \9 V L4 C# F ^1 b8 z5 F( F; C% j5 |( P/ W
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|