 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# e2 O3 d( @: y: U$ t) ^9 C# m, ]基本参数:' D, L- c# H; A! E( P
jta | thread | managed | custom.Class% \ z& b+ ?( K, B& e5 Z
主要是配合
; `# v0 E* ~, G3 pSessionFactory.getCurrentSession()0 b# k% A* o$ m& w4 r2 W3 K$ j# p7 r
来用的.; [8 {! M3 X4 L; m, {# ^
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 w2 u0 k' _# O
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ t; v) h4 a7 E
context.BeginRequest += OpenSession;
- {0 K( X8 e0 g% E4 _3 O2 y. a.Bind(HttpContext.Current, SessionFactory.OpenSession())
* ^( p5 Y. K0 R0 L% Z2 B9 x9 f% q$ S3 e$ c
//do transaction- R2 }' O6 Z8 k/ u4 c0 |5 |. C
getCurrentSession().Submit();: t9 x9 }& ?5 C9 c, R: ^
getCurrentSession().RollBack();
- I& F9 d3 L! z2 y" q3 k...
+ E- G" }2 w g% B! MUnbind(HttpContext.Current, SessionFactory)
+ D1 C5 h! y d7 w/ Y8 _
" R% y# F" f6 V! | {, mcontext.EndRequest += CloseSession;: \# a9 Q0 S& s$ t
0 r5 Z' P- }3 q6 r 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|