 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ K" }. c8 g6 z! u基本参数:
/ c4 `* E8 B; n$ m- l- vjta | thread | managed | custom.Class3 Z& M/ h9 o0 A6 S
主要是配合! @# I( n. ^8 ?8 e
SessionFactory.getCurrentSession()
& ]7 Y, ]3 P( A, Q来用的.5 K/ w) D8 F1 Q7 i5 b1 K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% D$ t! h& w5 j9 \" e* @9 o5 |
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 @" I/ ?: |0 I/ Gcontext.BeginRequest += OpenSession;3 y/ }* |& d% Z# X
.Bind(HttpContext.Current, SessionFactory.OpenSession())# b) W0 b4 D7 B. `; r& }' |/ F
; F6 T+ ?6 @& I5 o% }8 ]//do transaction- i) N( K. [3 v: p; F9 G
getCurrentSession().Submit();2 E: z; E. m! _5 \# g; n
getCurrentSession().RollBack();8 `- H1 A; |$ q/ r: {4 c- K7 x7 p
...1 {/ g$ t8 Y# b* e2 E
Unbind(HttpContext.Current, SessionFactory)
9 e! E: p% J. b( _# _3 M5 w! `0 l: i
context.EndRequest += CloseSession;3 N) j, w+ Z) S' O% i4 Z
/ B+ Y) ]1 @9 {9 G
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|