 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ u) l8 @( A) j( ^4 W7 K+ ^9 Z E基本参数:+ m2 W) j* d4 l5 Y/ A0 \
jta | thread | managed | custom.Class
' y, T3 M4 ]2 ]& y4 b% L( X4 m主要是配合
7 m4 q5 w2 {( b5 q" ]SessionFactory.getCurrentSession()
) `7 Z3 D& ]3 m5 ]来用的.
$ _9 }9 P# t! k( d( r/ {Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
|- C ]* t0 [1 AWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:# b1 E) O6 F; W; q/ y
context.BeginRequest += OpenSession;
7 q Q0 ]3 E5 ?! l- |' Y9 t.Bind(HttpContext.Current, SessionFactory.OpenSession())! i) U+ l( G7 G y' ^3 b: u
% X# w \" ]+ g, g8 w" w
//do transaction
) ^8 _7 g' ~ L5 l! A/ R4 vgetCurrentSession().Submit();
: _1 [3 T2 `0 }2 R" j( l" V+ QgetCurrentSession().RollBack();
/ a/ e9 u9 f' \( j- ?* ^" N...
P& |. l _/ t0 E5 ^Unbind(HttpContext.Current, SessionFactory)8 ]2 t! R1 E( L v0 p! Z; T6 U* G r
# i1 f9 ~* o3 |( c0 S6 c3 Z+ c
context.EndRequest += CloseSession;7 ~" G' J- Z1 T' \$ o. T% K
7 h+ R7 D/ C2 A: ~: h. u 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|