 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* E. a2 q; v5 q& Z: T基本参数:
, Z, X5 W; r0 ]6 P* _* C o/ Vjta | thread | managed | custom.Class: D& W3 d a. N1 H* x7 ~; n$ b
主要是配合
: o! ?) m: c, }1 D7 ?9 e5 _, F$ s/ ZSessionFactory.getCurrentSession()4 Q7 C. p3 }' F4 S
来用的.# H$ m+ b) v7 h% g
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,* [( W/ ?0 J. ?1 v1 H2 R' g6 O
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ P8 _) D& M0 |' g- w' ?% e
context.BeginRequest += OpenSession;
; V2 Y6 {% j* D5 i4 L.Bind(HttpContext.Current, SessionFactory.OpenSession())
3 m( B8 {6 q: C( Y0 c5 I: H# J m2 ?6 d3 A% f
//do transaction# b) C M0 I |4 Y$ B. {, [
getCurrentSession().Submit();5 f4 |- |' }% j3 H1 A
getCurrentSession().RollBack();9 g/ v- f, J0 i6 ]8 B6 [
...
& c% `+ ~$ s( H" y/ E$ c) d: [Unbind(HttpContext.Current, SessionFactory)
0 r, h& w) P s. q4 p& ], ^# D' R0 i8 J) b) ~0 l- b7 l. w8 Z
context.EndRequest += CloseSession;
8 F. |' W( A$ d# h* a1 t- P4 e7 @$ ?; _# k5 L: a
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|