 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; @: d! F8 o$ l6 g) ^& c: a基本参数:
3 y+ N3 R; ]* w; hjta | thread | managed | custom.Class
3 w0 ^9 t" A$ H: w, \4 v! |$ Y( Q主要是配合
- f v D! M% k" K9 x5 RSessionFactory.getCurrentSession(); l- C! s( L* H% K6 e: {
来用的.8 b4 F& [- z# w, W; n( H5 W M
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" H" j5 d( A* B8 J7 DWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 p' v7 P L; \ e T5 J" m2 @
context.BeginRequest += OpenSession;" S3 }* |- R+ U k4 @
.Bind(HttpContext.Current, SessionFactory.OpenSession())* Y. T9 d) [, {) J, f* V$ e5 t4 H
( J1 I+ d$ m( T//do transaction$ X% H. q( D/ w4 K5 l# q
getCurrentSession().Submit();$ ?, x. Y8 K* e0 a
getCurrentSession().RollBack();' E s( W" S) z6 G2 y) @$ L" V
...
* S0 N. X* Q" QUnbind(HttpContext.Current, SessionFactory)
& q$ w3 z) U' k d' ]# U
; p( j9 B$ s7 ` w. o5 Ncontext.EndRequest += CloseSession;. e% u) ]$ W& p) M: t
$ G, y/ w% e& ? 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|