 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 b& N+ x8 C U' j4 g0 A) a( a
基本参数:/ U9 R; |0 t9 v$ X6 s
jta | thread | managed | custom.Class
: j. r: K* E. `主要是配合; c' m) V1 N# [% M. [+ `
SessionFactory.getCurrentSession()
6 \3 F$ f# ~/ o7 Y来用的.
8 t2 |9 S: {* j Q; N; FThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- T( i$ ~( [* z+ p* e3 {% bWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
2 Q" q! y7 l2 ^6 [0 F6 n2 p: scontext.BeginRequest += OpenSession;
" g: N! u3 l8 H0 a a' z2 j.Bind(HttpContext.Current, SessionFactory.OpenSession())" S9 u8 x, g2 P, h; E
! }4 A" v! `+ k/ }* b, z4 R0 l//do transaction
7 y6 L1 o2 Y! U5 t0 TgetCurrentSession().Submit();
: A |( g5 B6 agetCurrentSession().RollBack();$ ]$ s7 }& w* r$ R/ i* {
...% g b% R% J2 \6 g
Unbind(HttpContext.Current, SessionFactory)
/ y/ @' G! z, h9 o
+ [2 o9 x5 N3 v) @# {' pcontext.EndRequest += CloseSession;* a9 R' Z9 Q* P8 L" A/ Z
7 K- S9 F; @1 T( e
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|