 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 \+ a5 Z; T! k( u: E+ Q基本参数:
: \! f; g. |: H' l6 Z1 f$ e5 Pjta | thread | managed | custom.Class
# p6 }$ [( i6 S# t4 Q主要是配合% c0 M! V1 X! w, R! w/ e
SessionFactory.getCurrentSession()2 B5 V9 N: U+ A, ?- s/ q
来用的.# y* x% N: B% M& v' F6 G* ]% b
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 E8 D6 x+ N. o& _: w* Z: e: sWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
4 r& f" N W( w/ B$ x8 Kcontext.BeginRequest += OpenSession;9 w7 ~/ H! F, M& p* a7 x0 \
.Bind(HttpContext.Current, SessionFactory.OpenSession())
) i9 Y9 K4 p- V: a" f
' y/ K+ k. Y. }& V//do transaction7 m9 Y4 M# j( A9 K% o8 R1 T9 |
getCurrentSession().Submit();
& }# J' [) O. m9 T k ]getCurrentSession().RollBack();9 e& E, ?7 r- O7 M. M* C4 Z
...5 O0 m+ n B* t1 _" }: Z. ~7 F9 U
Unbind(HttpContext.Current, SessionFactory)
6 F+ G- a) l+ l* L+ q& _2 m1 g4 |7 P
context.EndRequest += CloseSession; }+ V" b/ _3 g& X' h
& |2 l" W. `5 {! l1 ^& S/ u
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|