 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 B" l; o+ Q3 H5 q# S& U2 o基本参数:
7 F) `+ l5 n8 Y, pjta | thread | managed | custom.Class! Q+ r/ J0 B, s+ t
主要是配合: O C# ~- N( y
SessionFactory.getCurrentSession()1 N6 `' C1 p+ J# P8 J
来用的. ^: s$ l: s5 j& F' [5 ~, A
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 ~+ p* T' Q0 V/ c- I; d1 y R8 f6 \: Z, jWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:. d1 F) n" j2 i9 `& H
context.BeginRequest += OpenSession;' K* \& Z: T: K9 T
.Bind(HttpContext.Current, SessionFactory.OpenSession())
) G: x. X8 |9 J$ K7 P9 J) Y- z' _/ Q, W& E+ F6 z
//do transaction
6 a5 i9 D2 d# J. S/ ^! b! U2 K6 KgetCurrentSession().Submit();
6 d9 t7 i% l8 ^7 G! WgetCurrentSession().RollBack();
$ C* }3 C( X* N6 ?8 J, ]...$ ^& L/ t; Z. |/ w( g) u
Unbind(HttpContext.Current, SessionFactory)
: }& m E8 S/ E6 ~9 j) w# l4 t% V% e$ Q* e/ T
context.EndRequest += CloseSession;
# I' N- S+ U) K \3 I" Q3 ?" @% p; u( [! f- v) S
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|