 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what# H' W4 Z& u2 C/ {
基本参数:! J8 P/ W/ C0 T3 l
jta | thread | managed | custom.Class
1 F: ]) Q) g2 w7 `' g6 g! j1 T主要是配合$ W4 o1 f$ C' h8 F
SessionFactory.getCurrentSession()
% b' P& ^: U5 p% [来用的.% l. r5 }, f$ c4 B
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 ^/ D$ L9 q! }, r
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 M* D+ m V( z7 g. ]+ Ycontext.BeginRequest += OpenSession;
/ H d0 ^4 J0 y: V# y.Bind(HttpContext.Current, SessionFactory.OpenSession())5 C4 g' K, B& g" b3 |! f6 f4 T
6 ^# |1 t9 Y( A$ \# o5 ]8 _
//do transaction
" q8 n9 t9 d& [6 D) ^7 z, Z2 x8 IgetCurrentSession().Submit();' W' ?' y. O0 t2 w# `
getCurrentSession().RollBack();) l7 Q8 V1 l0 [1 ]# w7 d% P, s0 g( L
...
0 Z9 _! {2 F# Q2 }4 D" CUnbind(HttpContext.Current, SessionFactory)( P1 y% u; k, X: [
! E" H3 k! a! B0 o1 Scontext.EndRequest += CloseSession;
`" }1 E9 f e3 l& y6 y4 O! F; W2 F5 Q, q3 P* p5 f
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|