 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what* T6 v( O8 k7 G8 z/ H% x- P
基本参数:
) y0 B: S9 s* [: Y3 ajta | thread | managed | custom.Class, ]4 _3 Z' j( b# ~; p5 s% N
主要是配合
7 \0 D' q. Q' }. o# tSessionFactory.getCurrentSession()
R# K4 @% `# V* Y! I, o+ a7 e来用的.$ {, F; P: J. K2 A
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ m% t4 T+ X* f& Y: g/ L5 [& FWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ a- j, a8 m1 c h0 ~2 p2 l
context.BeginRequest += OpenSession;/ C% ~6 z2 L0 L0 d
.Bind(HttpContext.Current, SessionFactory.OpenSession())* d( R ^4 m, v
8 c7 [& c: b2 ^! O" d7 o4 B) P
//do transaction7 w s* W* H$ x' E: h; I
getCurrentSession().Submit();
* d8 u( e: N0 f2 pgetCurrentSession().RollBack();6 _ t4 v" C1 k
... w5 [6 d8 |2 W2 B% y' e
Unbind(HttpContext.Current, SessionFactory)
/ N) ]; ^3 e, p6 C- D* ?4 P# P) q4 K: c0 E4 x6 n/ w* I4 S2 i0 G
context.EndRequest += CloseSession;: j* j. _4 `' W+ j9 v' ?; d
4 J4 i5 P, m* c, s% t 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|