 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 W2 v! y- ~: W8 v
基本参数:
" O& A7 P4 U* ]0 Bjta | thread | managed | custom.Class
$ t) ~: h* b9 o. Z# ]主要是配合
+ W& [# W* g. S! CSessionFactory.getCurrentSession()
: |' l* A% z& W, v来用的.; u8 o$ w- D! ]
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; S6 V9 z+ z, o
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 g u8 f8 F' g3 m' Jcontext.BeginRequest += OpenSession; ]% R: g- | Q1 {+ B
.Bind(HttpContext.Current, SessionFactory.OpenSession())
) Z- m; ?) e( o7 r0 z H& Y8 \1 Y3 X6 f6 m+ A1 b
//do transaction2 f. G. A; L0 N% P* [; X; \
getCurrentSession().Submit();
2 e5 ^; O/ L% a5 m& u# O; {1 ZgetCurrentSession().RollBack();1 [8 ?* e" G7 s/ Q* c( F
...
3 x- D6 ~6 ]6 T* y8 W, a9 }) {Unbind(HttpContext.Current, SessionFactory)
; V8 b3 i$ L- K4 D j
7 m0 |9 v3 k& ?. o2 U/ Jcontext.EndRequest += CloseSession;
2 J8 @3 z! F- [1 m( A( I/ X. U" x3 V: ]: Y& }$ A
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|