 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* L! ]! c, k$ H% q( n基本参数:
/ x, o8 w- d! L4 e9 Qjta | thread | managed | custom.Class
8 M. {4 v, S* w- P4 l$ Q主要是配合
: T$ e1 w( r3 R- V3 V8 i4 G# N5 bSessionFactory.getCurrentSession()! Q, y0 f' U& }% E9 V, Z! Q
来用的.4 `# C' T& F( W8 ]; S9 e
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 s" }. G9 a9 J/ R4 d3 ?Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 h7 Z0 K2 f: W8 C2 _6 v; D
context.BeginRequest += OpenSession;" a. D% ?! T0 V% N& _. K3 ]
.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 J* e4 j0 a% J8 X# q
& U( N5 c9 L( y4 p b+ v, G9 Q//do transaction& t# h* A' F& m( _; L0 G- Z& U9 ^1 E. b
getCurrentSession().Submit();
# \$ g. G+ e* a* x% |9 zgetCurrentSession().RollBack();' B! V& p: b& _1 T
...9 g" I& G, R7 M) m1 ]) K' ]6 |
Unbind(HttpContext.Current, SessionFactory)
; {: f2 Y4 `$ R9 p7 f+ Z: ?
9 ?+ n$ U" C, O- ]4 L! Qcontext.EndRequest += CloseSession;: F% ~4 H3 f& M$ R1 d5 r6 P' a
! D* t, U! R0 P% n$ w
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|