 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; F2 M0 @. k3 G1 r( w: ^ Z
基本参数:% c" y& i. k3 s% `" }) ?1 J v# |- \
jta | thread | managed | custom.Class+ A( [ X3 y* `9 g$ y" r! R
主要是配合4 z& a" D& ]5 l6 H/ J* s' ~! _
SessionFactory.getCurrentSession()
5 }8 N9 F _! C) F; f, [来用的.
- S( e7 l+ \3 F3 ~( X/ nThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" f9 t! }, w, j$ DWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 Q' v; J* `4 o8 c; [* |
context.BeginRequest += OpenSession;0 p4 X" I( n0 M8 U; v
.Bind(HttpContext.Current, SessionFactory.OpenSession())
" Q- u$ \" P2 ?7 U- C
7 f7 x4 b5 U# \+ F8 q//do transaction
+ h" @2 A% t6 s' @8 _! RgetCurrentSession().Submit();- B0 Y: q" E% d) ^, z+ O
getCurrentSession().RollBack();
) U+ g; E3 r% u...1 X4 ?# q" m" j
Unbind(HttpContext.Current, SessionFactory)
4 {8 r- q" m6 s' o% f0 w# m
* L% ]5 t" ?( Z5 J* Qcontext.EndRequest += CloseSession;
* f* Q6 M$ w* s9 C( B
5 U1 E8 n! w: k/ R5 t$ X G+ r" A o9 z# d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|