 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! e2 `8 {* B: D7 G! U" [9 U
基本参数:6 T4 I5 R( {/ _7 v
jta | thread | managed | custom.Class0 n: X1 ?4 o" d; H* V% e
主要是配合
3 W& e W6 E$ \. s9 CSessionFactory.getCurrentSession()
& f) ]$ g$ q: U' ?7 b& G来用的.9 Y# ]' G( B3 s. s4 Y& C) e9 v
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,4 F2 j/ Q( T* M2 F% p+ U; B
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: Z' I/ I9 ]! r! x1 q8 v$ N! D
context.BeginRequest += OpenSession;
: S4 b' O+ C$ f! |( T.Bind(HttpContext.Current, SessionFactory.OpenSession())
! D2 p/ ]0 ?! E& w, \" d4 H5 ^5 }- r0 M6 a* N; B: F
//do transaction8 s" `/ `# E* i
getCurrentSession().Submit();% L1 W4 F9 y$ s7 V' h
getCurrentSession().RollBack();
9 k0 ~$ [9 C+ _' C* Y! O...* S" S' q) }, n# j: ]# @# k8 L
Unbind(HttpContext.Current, SessionFactory)
6 {# r: K1 e# }: X% u! j ]( o9 Q% J( E1 {& J+ ?# M
context.EndRequest += CloseSession;
+ P: V8 P! m' y+ L
# Z4 x+ }7 `3 \9 Y0 [, ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|