 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 x3 w2 G; N; l4 q# D. F7 E
基本参数:
! |; z2 f2 m0 o9 C! x9 I" b. k; @/ Ljta | thread | managed | custom.Class3 j. m' z" S- j, f3 X4 E
主要是配合/ J; j! W" l1 f! ~) F* r) F) k) m
SessionFactory.getCurrentSession()
& w4 c- [ W2 r来用的.7 [$ S! U1 l9 J" Q7 r
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,5 y6 t7 u( G6 N. J
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 d& D9 f- k) p: J0 w7 Q$ U x7 {
context.BeginRequest += OpenSession;6 ]$ s( b, I f5 X
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& k$ R7 \+ t' m0 ^7 X. v) s- F3 v2 P% G
//do transaction" j* F& K- {' Q y9 y
getCurrentSession().Submit();
$ x- N; _% Q* lgetCurrentSession().RollBack();& s( A4 h, d4 h X R6 W* t2 k
...% f, B0 z1 C; y4 G
Unbind(HttpContext.Current, SessionFactory)4 h4 u' q+ m9 u. V8 l8 ]8 Q
! q+ V; \8 u- s1 o/ e) I3 |
context.EndRequest += CloseSession;
' L. N4 m- m3 U1 z
2 P; P8 O U8 Z% [) B 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|