 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
) v: J L# p- a% t基本参数:* O3 H* y- o+ o5 _4 b
jta | thread | managed | custom.Class
7 P' _ F$ a8 A1 a+ ?7 l主要是配合- v% V' n/ j$ i. M% E
SessionFactory.getCurrentSession(): O3 `# ?) L6 M0 W0 [" R" U' d
来用的.
! D; b0 u; r( D4 b5 b7 X% @3 EThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% J7 j% Z* v! ~' B
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 N% l( t( z) q+ l4 \2 Qcontext.BeginRequest += OpenSession;
. Q. X$ r3 X0 l; g0 Z# E) [.Bind(HttpContext.Current, SessionFactory.OpenSession())& q/ }: }3 D, c8 v0 K" w
% R9 P6 L0 \* P) \0 ~//do transaction. e; b5 D3 V. }% n9 ~' Q, w# |; G
getCurrentSession().Submit();
7 O) [6 \, n. Z6 w7 Q4 m, ogetCurrentSession().RollBack();+ l; S3 s' y a5 A# D1 Z/ n: B8 v
...
5 p7 A( X+ j# g) t& f! wUnbind(HttpContext.Current, SessionFactory)
Z, l: g( z" y- `5 u5 m3 S
. e5 e( R+ n' L) C2 K0 Wcontext.EndRequest += CloseSession;: t; Q0 i8 W @6 M1 {
|- |4 C: B! f6 E1 d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|