 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what i5 ?% D* @+ s4 t0 V
基本参数:
$ k' Y8 [( i0 v( s0 Cjta | thread | managed | custom.Class: u% B, j* m* c. T! e
主要是配合
: b: I% R& [; }- X/ a p8 xSessionFactory.getCurrentSession(); z3 z9 n7 C1 ?# A# g$ f
来用的.6 w; w& M% C/ @; j8 H% m
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* ^# \( Y& }) U. O5 ?7 w- JWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; W+ O `) W8 H2 M j4 H5 d$ Q2 Wcontext.BeginRequest += OpenSession;7 @/ W3 N1 e% T" N+ f7 P
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& p; y: }9 U. b% u- @2 k( Z" ~$ O( }' s% P' X4 W; |' E
//do transaction
! R' ]) f1 {( Q2 G; ZgetCurrentSession().Submit();! W3 J3 x6 b5 j' t1 X9 _% O
getCurrentSession().RollBack();% ?& W) M2 i. E
...* X8 }6 k9 q" w8 u+ f/ T: E
Unbind(HttpContext.Current, SessionFactory)" _) H1 R4 B! Y- z1 A, K
9 D; W( X. f/ L. l# Wcontext.EndRequest += CloseSession;
: W1 g. b8 M5 n& ?3 O, r9 c" o) T2 M3 E4 a: M
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|