 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ y9 \8 Q5 J, T9 S) }+ l
基本参数:
. u# n" T6 h/ Z0 _jta | thread | managed | custom.Class6 D/ g: {2 V3 s# [) J2 {% e
主要是配合
1 \' V; W" W( F, B0 zSessionFactory.getCurrentSession()( U9 Q! V9 ~0 L9 D1 m* u0 ~
来用的.% t! M( c. Z, [8 n! C" g
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
3 g% N0 {9 Y5 Z' a9 }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 M% E0 T4 L$ g/ ?( Hcontext.BeginRequest += OpenSession;
; F9 e* v! @9 I8 p' |.Bind(HttpContext.Current, SessionFactory.OpenSession())- V/ a( e' i. M' j
- g% F1 h& e1 m. t* k+ V8 u//do transaction# o2 T, Q/ [; g
getCurrentSession().Submit();) ^1 c1 _1 d: A4 H, \: w2 Y
getCurrentSession().RollBack();# x$ f$ O+ J: f+ D' g& j
...
5 h. j4 {7 ?" C! G6 ^) n: wUnbind(HttpContext.Current, SessionFactory)
4 C( c+ Y2 N4 w! k+ T
- B: L6 B% v0 k& l( {context.EndRequest += CloseSession;
0 K+ j( Y I# M8 X; e- h( F
, Y# O$ U" w2 k9 Y/ `$ z, D 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|