 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! T$ A3 f! m1 b9 A8 P% L0 R- [基本参数:
+ J! l. z8 T& q5 V$ \jta | thread | managed | custom.Class- Z4 y+ P/ R! T9 f: e: H
主要是配合
( G. s Q1 _4 q% I* _1 B2 oSessionFactory.getCurrentSession()
6 I) f8 P4 a7 n4 T1 U) D来用的.
2 {3 s5 v% o( L+ pThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; }6 x$ m' r: T, MWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; {8 u+ K1 b6 f3 L# x5 Zcontext.BeginRequest += OpenSession;1 o; ~- `0 W# n' U& r
.Bind(HttpContext.Current, SessionFactory.OpenSession())( p: q( M) J) Z7 O+ f8 ~2 R l
* J% U' }7 q" c/ H5 Q
//do transaction$ Y$ @& q# Z% o1 y1 R4 y* o! h
getCurrentSession().Submit();( K. x, x6 h/ f8 |) V) l0 ~
getCurrentSession().RollBack();
+ @- s# X- {4 j u...: U- E: s1 g: {
Unbind(HttpContext.Current, SessionFactory)
; w) q) z7 ^6 Z1 y* E* g% K1 [) Y) C" a. A
context.EndRequest += CloseSession;& P) }1 f: r- E$ L) }$ `, s; E
; W* s @9 o8 x+ c, m: `
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|