 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what0 k( D$ X+ u& l, {: U
基本参数:1 O* W3 x, C% S/ _7 }
jta | thread | managed | custom.Class# f: L! Y8 u* @+ W
主要是配合
9 [/ ^9 }/ }/ {3 RSessionFactory.getCurrentSession()
! _0 V% v8 @. L- X来用的.
1 M7 F- V F# u. x* j) wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
& k+ t4 M' e- A$ HWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ E3 g& M- w8 [. T# hcontext.BeginRequest += OpenSession;' r0 i6 {* V$ d8 C2 S% O+ G& B
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 ~+ k7 N- R. u. o# J1 s
! f; N7 b1 V# n" y W- n9 g, r4 Q
//do transaction
; [4 ]9 _# j# G6 j1 z( KgetCurrentSession().Submit();
. g `& }6 c) u. h. ^getCurrentSession().RollBack();
" R* a2 t/ q" i" j2 n7 s...8 ^& `" V/ l3 ~4 f5 v
Unbind(HttpContext.Current, SessionFactory)/ N% m: Q) W: w. u5 q
" Q; K1 N9 l; u$ f0 a) ]% W
context.EndRequest += CloseSession;
2 `% V* S* p7 d
9 }4 d7 C9 ~" d/ {( a7 i8 d+ ^ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|