 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* p# @8 R# S6 o! n基本参数:
0 M9 K( F8 B+ [* }# d1 Cjta | thread | managed | custom.Class+ e& V8 t# A+ H% s- {1 ?
主要是配合
! v& \( _5 |, `4 F0 V9 OSessionFactory.getCurrentSession()
& L2 v. U2 V: q* z5 p9 w来用的.! k/ k4 ^ p- `
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- F+ J6 ~8 r6 E$ S( U1 \. s
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 x/ l. R8 @& B. l; H4 { i$ ` [context.BeginRequest += OpenSession;
* ^ H+ N y- ~5 | Q.Bind(HttpContext.Current, SessionFactory.OpenSession()). `! q/ C4 R0 R( M+ s
. R% J* p" A& z0 y7 p) Z* W- f
//do transaction7 W8 z# ]! j% U9 X* W
getCurrentSession().Submit();& S" I2 d6 ~: @# o D
getCurrentSession().RollBack();
! g5 @/ ? k2 a! l3 B. H; [...
R, h+ z; \, |+ ?Unbind(HttpContext.Current, SessionFactory)3 z- J2 X# { P9 x' v- o/ ]( W9 W, U
7 b; D) v! B% }
context.EndRequest += CloseSession;9 i. l4 v( F6 q f! T$ z. q
+ X7 M+ w9 ?5 M% r. O 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|