 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what( X) F2 h( o7 V4 X# Y3 [
基本参数:
u! H6 ~) a W& G" { [9 djta | thread | managed | custom.Class1 E6 V2 U( A. _& x2 Q/ z7 p9 }9 E* z
主要是配合
- T5 _! N) \7 D, z( y( dSessionFactory.getCurrentSession()8 K) Q' I- D* r2 W2 |7 A
来用的." ~/ [0 R" ^: w8 Q0 X. ^, }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' m( T; |$ r* \7 L
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:. S0 I% X8 _/ @
context.BeginRequest += OpenSession;9 P4 k2 Q1 {" [# g+ ^
.Bind(HttpContext.Current, SessionFactory.OpenSession())% G. r' v+ P' O$ f, g1 x
) F$ x8 o" g Z
//do transaction
6 K$ @6 V% z' a4 E$ l7 [getCurrentSession().Submit();- @& L5 f3 Z2 u
getCurrentSession().RollBack();$ m% y+ g2 W2 G* \/ g8 |
...2 D5 ^8 Z$ Z4 m0 }" A; X3 N
Unbind(HttpContext.Current, SessionFactory), d8 }1 \7 X# R ?2 q- {
7 v; L/ S, D& k6 I! y. O
context.EndRequest += CloseSession;
3 R2 H6 j4 N0 w7 s, D/ ^, @
- D' i& i7 }: H }9 R 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|