 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 j# a8 {7 j- B) o/ i基本参数:6 M5 I N, Z5 R( z4 [( J
jta | thread | managed | custom.Class
) j1 u/ ]8 a6 m; p) W: w主要是配合
/ |' g# l8 [& OSessionFactory.getCurrentSession()
" U/ L1 n, a4 F; E: \来用的.$ U" k* b1 r$ [$ f6 y J1 z9 ^
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
. J. w% W9 i5 _8 C/ q$ SWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:3 `+ j7 ^3 U! z7 ?4 p
context.BeginRequest += OpenSession; `3 N6 n5 O$ B/ J- w& P* A
.Bind(HttpContext.Current, SessionFactory.OpenSession())
" P4 C+ ?( g1 M8 _0 z+ [! K! y# V+ i4 C) R- t1 c, i8 t8 U
//do transaction
8 U) g6 n y6 ?4 Y* [ L0 \9 LgetCurrentSession().Submit();4 y* L9 v; z, z4 k1 c: e- h
getCurrentSession().RollBack();( v" m, ?! H4 g( ?$ P: z1 }; \
...- T+ L& L' u3 M9 z$ a! t
Unbind(HttpContext.Current, SessionFactory), S$ t4 s$ q9 ^/ S
$ K/ Z) G8 d' J& C" v
context.EndRequest += CloseSession;
# a) G$ w# k& S2 }1 c! g; A+ e/ q# o% C! R+ V
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|