 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& S. n5 p# Y Q4 ~, u基本参数:3 A0 P9 h1 N4 ]& l
jta | thread | managed | custom.Class) m$ \0 O3 B( ?; N
主要是配合
9 D# Z1 S$ ]; H9 `5 ]$ ^' N) USessionFactory.getCurrentSession()
: @5 L+ D8 p( K% o9 C6 W来用的.# O) S- N. V7 f5 e: ^
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) n% s0 Q, m' GWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:+ _% e: p3 G9 \( T" O" m% _
context.BeginRequest += OpenSession;: i: F t& L& z2 Z6 D3 i* H
.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 E+ P! [6 I( n( h; U, W( l+ ^. p4 ~9 X
//do transaction% W" [6 y# X. d: x/ P" y
getCurrentSession().Submit();
( G5 w0 L5 P8 `/ z' d1 E9 JgetCurrentSession().RollBack();
% D) e/ T; i+ |! e...
# Q& t- O4 R* {. ?9 xUnbind(HttpContext.Current, SessionFactory)
2 j8 y- m8 J. q% ?1 x! A8 W
! O. @4 B1 t, {3 gcontext.EndRequest += CloseSession;
, u2 s( y3 R* f5 J s$ y0 W5 K' F* X
1 Y7 s+ ?5 J) a' f9 q! f, n 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|