 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! E/ v2 u8 Y! k% k j9 t9 e% T
基本参数:
7 Z' ^1 \ ^9 e8 G, A# C5 Y" E0 |+ Fjta | thread | managed | custom.Class
' t8 ?# t- M9 {9 L主要是配合; i4 ~& x) M6 ] L* f$ `# X P
SessionFactory.getCurrentSession()
5 ^; s1 ~9 Y; i; w0 X来用的.
. p* @* ^- ~8 j" d7 i5 h0 nThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! a7 W' @1 y8 G- Y n8 D, W
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& Y' l6 v/ L# ~. [1 C
context.BeginRequest += OpenSession;
( t3 Y* F+ ^9 O* ^& {6 C- V/ ].Bind(HttpContext.Current, SessionFactory.OpenSession())
( Z, |8 g$ ~5 s% I1 d( X3 T3 o1 B0 l2 I# A# Z
//do transaction- a- P9 ?& M8 q' u
getCurrentSession().Submit();6 o4 M& k& K @ @7 l
getCurrentSession().RollBack();
6 i# J; G# F$ L8 ^! D. ?4 z ]...2 G9 L8 c; _- S
Unbind(HttpContext.Current, SessionFactory)
: K! T7 x, T, M7 q
! `0 g6 m( R4 { U8 T1 zcontext.EndRequest += CloseSession;* Z3 e l+ \2 t0 [
9 _- z& W* Z2 T0 X7 `; l 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|