 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- M0 ~$ n& E1 L1 h0 A3 o9 [! d9 @8 y基本参数:
' d( c7 U% q* l9 G& \0 D$ Bjta | thread | managed | custom.Class
1 _1 s1 @6 j- v; M1 B主要是配合
2 k2 i$ D! e* J! o$ P: A# T& ISessionFactory.getCurrentSession()
* a. D! X* t/ [6 X0 Z来用的.. b" o0 T# i K0 @8 a+ E5 z# c. }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 T' }- a" K- Q8 X7 A3 [5 n$ u
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ o1 t1 t$ X0 }- _, |
context.BeginRequest += OpenSession;! b6 v: _9 { `6 m2 v! J" `
.Bind(HttpContext.Current, SessionFactory.OpenSession())7 \' z7 Y% _0 f+ h
2 B9 |& F# L5 {) G9 w: y7 k, [9 c2 N
//do transaction" E. U& p5 a' E" P6 O% r
getCurrentSession().Submit();1 O2 d: m& b3 _. P
getCurrentSession().RollBack();; P# V9 V" @# w( b( `8 v
...* [$ j+ T6 R( q5 Z# a
Unbind(HttpContext.Current, SessionFactory)4 a1 ^6 S+ b& T5 [! H* D
7 @* |/ k* L* p: ]+ k7 Vcontext.EndRequest += CloseSession;
/ w i% Y$ }, ^$ u R! r6 K
; d3 x1 k' F5 U4 Z$ B) f8 h X 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|