 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) \, L$ ^: e& c
基本参数:
$ |, p) ?& c/ ?7 @% ^jta | thread | managed | custom.Class& y3 a6 J/ H) a3 i& d- t; X' ~" k
主要是配合* w2 l: Y; U7 ?9 x _
SessionFactory.getCurrentSession()
0 X; e' p; w# _- n/ s$ N来用的.0 P) w% \8 W; {- ?; R& g7 a
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确, o2 p8 U) c2 T8 D. P
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 N D2 K0 I7 K6 Y
context.BeginRequest += OpenSession;$ X% y( E9 w$ v- ]1 E: _
.Bind(HttpContext.Current, SessionFactory.OpenSession())
/ i- z9 `$ c. L3 p; F+ T6 M1 g |. H" c
//do transaction
2 Y; Y3 x6 |$ W! B. Z( zgetCurrentSession().Submit();
+ L" k p8 c' ~7 h9 i; F5 \ ^0 igetCurrentSession().RollBack();- W' B$ u! z* \
...
! s) K5 Y& {3 ]" b2 t* JUnbind(HttpContext.Current, SessionFactory)
, s0 x3 g! y; r5 S4 E- E! n' h' L; B% J
context.EndRequest += CloseSession;& O- A. Q7 _8 t: e3 w
6 s i0 ?' {; `& W% l1 Z7 s
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|