 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
8 p1 O. \ C% D8 o; \基本参数:, Q, N! ]( n9 D
jta | thread | managed | custom.Class
7 V2 ]. i3 a0 o [% B! V主要是配合
" s' D2 j. @2 |: g% o: \/ S" ISessionFactory.getCurrentSession()/ ]4 D0 k8 x, a! S( P
来用的.
2 I" B- `" T# l$ v/ A* KThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 i" o O7 G) L' P
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( Q, N" H" e6 B' K$ a3 tcontext.BeginRequest += OpenSession;1 J) b( g2 t% G+ x+ S- T
.Bind(HttpContext.Current, SessionFactory.OpenSession())7 k# U' O/ @6 G& j! H3 ?/ j0 k" F
( q6 P! n3 O) K6 g//do transaction9 A) @! K' R y) z: \# l
getCurrentSession().Submit();
) v. ^: m7 z, `0 I2 LgetCurrentSession().RollBack(); T7 N% @ x4 v8 k2 u3 W
...
: u/ D1 \) |3 u+ z+ [" ?' ZUnbind(HttpContext.Current, SessionFactory)
7 Z6 U4 i9 v5 Y" o: s" J+ x* w0 ]( h4 I) W) c3 j$ A+ ^# k& z
context.EndRequest += CloseSession;
) ?8 `. i4 \) b! r6 g2 t' u/ t K
! c \. H3 ~# H- n- @ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|