 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) _) ~, b1 A' s( f6 W8 b
基本参数:
( m5 [% N$ {6 q P" `jta | thread | managed | custom.Class2 G) l$ f0 P; J* z- x( t
主要是配合
- S# ^/ @9 q+ _7 Q USessionFactory.getCurrentSession()
0 `# J6 g( u F. Q8 O: r( M( d来用的.
" M4 ?: M2 I4 Z& m! UThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ D/ C2 B; B" B8 W; G
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 T9 b8 G) o' I) }3 x. G, Vcontext.BeginRequest += OpenSession;1 }1 ~5 b* I j: X" n! I
.Bind(HttpContext.Current, SessionFactory.OpenSession())
! P q! _# [( l5 x$ l8 B1 W2 {8 u# H, L- U& } V: X1 [
//do transaction% H' Y$ _7 Q! f! v
getCurrentSession().Submit();
8 K V+ h! U# g! n* IgetCurrentSession().RollBack();
: a9 j: I0 F( @; }7 ^4 ]...& q6 V% o% h2 ?: K5 B
Unbind(HttpContext.Current, SessionFactory)% @% b9 i! C: z: x
% }9 _4 T+ p# A
context.EndRequest += CloseSession;
6 n" n6 n, b5 H( F1 V& a' C& v7 X: V: o5 g
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|