 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. ]0 y# `% O( a7 ]5 F基本参数:" h' E( N3 d2 A5 r9 z
jta | thread | managed | custom.Class
# J' J# }3 z9 L$ D主要是配合
. t; e$ j! r8 V" \+ O: X: `9 ^SessionFactory.getCurrentSession()+ T5 E, t9 k$ e8 X* a! E6 j/ Z
来用的.
' o$ k$ s+ u' p" @Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
5 @( M4 c: F. T) k7 I$ H8 qWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:: s$ X/ L, d, f
context.BeginRequest += OpenSession;
1 R/ q8 J. M5 Y2 G: B# r- Y.Bind(HttpContext.Current, SessionFactory.OpenSession())
& }9 l1 i. O. p( K Z! J# `* g9 i
" G. J/ v* `4 ^, y; Y//do transaction! U) ~- d( n# \, S4 u# h: U
getCurrentSession().Submit();
- P4 s; |; X8 ] pgetCurrentSession().RollBack();% l" y4 J4 u& `# L* ~5 W" t
...
5 Y3 g5 J" T; p. C$ F) L+ kUnbind(HttpContext.Current, SessionFactory)
1 {3 n" B3 C6 G5 q+ W- ]4 A
3 J; @) C9 P+ \2 Y/ H3 I* Z4 W+ rcontext.EndRequest += CloseSession;
( f% d0 {1 G1 d, f; \
, {( x2 J0 G1 F: t& p9 N* L7 Q 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|