 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
8 ?3 [& y, L3 {% z' i0 Z* @! i a基本参数:
P5 W1 G6 f- Q Zjta | thread | managed | custom.Class! e- _! V% x$ {5 v* o( H
主要是配合
- F: Y3 }. a& P& c2 H$ P: J, V4 f( a" ]SessionFactory.getCurrentSession()
* g; ]/ @' }, u0 d- _; {" l( F来用的.5 t; T" ?5 v; l, y, }( ]9 J3 H4 q: s
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
+ m. z! h5 i' p* ~) r! cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:" [! ]& w! i$ a
context.BeginRequest += OpenSession;
5 A1 }# f I: K3 [.Bind(HttpContext.Current, SessionFactory.OpenSession()); d6 L7 Q7 o0 ?4 R) X
6 u8 o6 a" m v9 j//do transaction+ t, f+ Z2 R. t+ w# q( X; i2 [- b$ o
getCurrentSession().Submit();3 M7 P5 S* q" {4 R: v4 z
getCurrentSession().RollBack();
/ l8 A0 K6 y5 Z8 r5 J...
4 i6 d. W5 O, K8 J V4 nUnbind(HttpContext.Current, SessionFactory)/ T* D1 e$ v: C
2 h6 X+ @5 E( ?! Pcontext.EndRequest += CloseSession;5 a. }0 E( u; {4 x" U$ A0 Q( W! h, I6 P
7 t4 L# m F: e: w; |: s 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|