 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what- ], M1 @) \* W. `) b: @
基本参数:
- V9 B" N7 ^& v8 G; c9 u# djta | thread | managed | custom.Class% J$ y4 ]) f* x* L9 ]
主要是配合
% o2 o+ `) s3 q6 Z( |* T3 P, U5 [1 ySessionFactory.getCurrentSession()
" J( ~$ R) U) i, I0 x来用的.
0 j* _+ {5 f8 Z* {& N# @- dThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
# ]6 F3 u8 }/ L; KWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:5 _6 m( Z* f( @9 Y2 `( A
context.BeginRequest += OpenSession;7 v; E# T5 G, N8 y8 ]
.Bind(HttpContext.Current, SessionFactory.OpenSession())* y7 X3 E" @8 \$ _& v4 c" H0 |9 w
7 q+ l* k1 Q. }0 V: N9 {) H//do transaction9 }6 p @% e& S* w* O+ v
getCurrentSession().Submit();4 N% `! S: ]% ~) ?, o
getCurrentSession().RollBack();0 U% @6 A2 @+ V
...
( T0 D: \' V% T* F- v8 TUnbind(HttpContext.Current, SessionFactory)+ R! J- F! S, n5 P# j$ u
; @) I( P- h: N& X- y$ Y8 Pcontext.EndRequest += CloseSession;
: Q' l5 y9 K) p7 x& J
4 Z5 | \$ I; m% ~. X9 G) A 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|