 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ I0 Z2 D/ u {7 A, l6 Y! A
基本参数:$ u' f8 j1 m2 f2 f; Y
jta | thread | managed | custom.Class2 [' @4 Y! n4 T5 h/ u! e3 \
主要是配合
; d4 ]% O% r0 y. _( |& S$ LSessionFactory.getCurrentSession()& n) J% u) l% q8 W
来用的.
9 v0 ^: r# f1 C! z8 [& z: ^Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 G& p* J$ g* N t
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 m, Y: W8 t0 n9 `5 Econtext.BeginRequest += OpenSession;6 d& O$ G$ q* W4 i- Y2 b8 S5 j; K
.Bind(HttpContext.Current, SessionFactory.OpenSession()): q0 X5 H3 Z+ \
: e6 ]0 A/ ^" E, ]; a' t# t
//do transaction4 m& s% f/ k: `$ j8 l# y
getCurrentSession().Submit();
# e+ O: s$ I) O7 t- q& RgetCurrentSession().RollBack();' ?3 |' g; [! b& b, y
...
! q5 i* N4 w" c7 Z( @4 Y2 D4 o& bUnbind(HttpContext.Current, SessionFactory)
$ E) b$ d* P& P% S+ B5 R& N% t* R& s/ W
+ O% o* h, W1 kcontext.EndRequest += CloseSession;
1 t3 }. {' F, u& F; U+ m/ h/ |! J t
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|