 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; x4 d4 k- f/ o1 r, m8 N( ?
基本参数:
2 L9 `0 S. N* x2 ]$ v* S! Wjta | thread | managed | custom.Class/ }4 g! Z' X; A1 S4 [
主要是配合- p N. i; { O5 X& L( B
SessionFactory.getCurrentSession()1 _1 O+ o2 d; q8 D3 s2 v# u7 x6 e
来用的.
5 u+ x. _( E. ]5 u; \1 W% x2 wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; K! H+ [! B8 R" `1 A5 M
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ s. M, S8 z# y1 {: Q( W
context.BeginRequest += OpenSession;
8 \* ~" Y# X3 J.Bind(HttpContext.Current, SessionFactory.OpenSession())
% K& ]0 b( F0 A& P$ M. a
& S: Y3 u4 `/ |& ^0 G' Y//do transaction& J0 S; u( p/ U) ^
getCurrentSession().Submit();
. U D2 U- O) LgetCurrentSession().RollBack();
% v/ }# y2 D% Z* q t+ F...* p2 Z; h& }" n1 l
Unbind(HttpContext.Current, SessionFactory)( q. v" a) Y. ]# s$ f% r4 O$ Z5 B
6 [$ c8 I8 n c0 K0 y7 T) scontext.EndRequest += CloseSession;! E" g% r9 f9 G! A8 X8 h' Q# Y
Z0 p/ R+ R; @5 h" @# j
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|