 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! v. T6 X0 B& W1 L6 M, {
基本参数:
1 j: v& m. [7 X' D1 s5 U5 zjta | thread | managed | custom.Class
9 ~2 E8 f3 @1 A3 k! d- j# m8 z, f主要是配合
$ v! Q" J6 H, DSessionFactory.getCurrentSession(): @' K* o5 ?- F2 L
来用的.
1 L+ W, `& V3 ~# `" \Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
, j+ ~- a3 [* v5 R$ z7 BWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
0 Z1 u* m1 ~# J- L1 Q% s7 ?4 a$ }context.BeginRequest += OpenSession;; m% k; `6 M1 B! X7 C" O9 k
.Bind(HttpContext.Current, SessionFactory.OpenSession())1 S `1 W7 O) V2 J
8 } |" V& n' N0 F, ^//do transaction0 A3 d5 \# T K- f! c3 K% r
getCurrentSession().Submit();
4 Y1 n$ m$ V; a2 i8 h& kgetCurrentSession().RollBack();% ?2 b' c6 \" J: Z1 ~
...- N. c: }/ i7 P2 J
Unbind(HttpContext.Current, SessionFactory)
1 L8 z7 u0 u$ K& z/ `3 x J* i0 ^
, @. `1 G% Z [4 O, ncontext.EndRequest += CloseSession;9 l2 j, |6 Y' Z, k0 u
4 T3 O# u t) W, P2 v- e2 D6 l! U) t
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|