 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ ]: ]) I! J! G% F. c基本参数:( D# ~% i: k( Z* u& H0 [+ x5 \
jta | thread | managed | custom.Class
# s2 ^5 V2 G& _$ }主要是配合
/ z, Q9 z: j6 Q |4 \4 a: K2 DSessionFactory.getCurrentSession()5 l6 U# T6 L) O7 @
来用的.
# H! _2 b. |( J2 }& |# \/ iThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 k _+ T5 u6 w* y, `9 x/ r
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# {" H; a; S% o7 A! s1 P3 Qcontext.BeginRequest += OpenSession;
& t S: p/ ~! C7 o5 z# S.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ l% {* e( o$ G& ~5 t
* |3 |( \4 r& d//do transaction
- E; z3 r, t8 YgetCurrentSession().Submit();) x9 p8 E& ^" [! @5 T
getCurrentSession().RollBack();
6 M/ X- s& x' N+ I' h! K...8 W/ I6 o% n( i2 J5 U1 h' ^5 `
Unbind(HttpContext.Current, SessionFactory)
9 o/ U4 j) q' Y7 o0 Z f
" S. t3 N9 g3 S! J8 Y5 X+ R+ @context.EndRequest += CloseSession;
; B# O7 g, ^6 w a, q. u4 s5 E+ x, l
3 P* b9 d* p3 z+ x 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|