 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what- e5 q% f& x' @- K: C
基本参数:
6 A: q- S* `) h' F' ~; R, Qjta | thread | managed | custom.Class
( s7 _( F$ T4 @ k$ p* J主要是配合1 M% L+ @5 F6 ^3 R3 D
SessionFactory.getCurrentSession()& K! ~5 ~! E% |. E( D
来用的.6 v8 W/ |/ X O7 m
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% b) t/ Y/ d' \; s0 t9 b3 @
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! P0 Q9 z! E7 Wcontext.BeginRequest += OpenSession;, O' e) X# l3 v/ t/ L1 a, P
.Bind(HttpContext.Current, SessionFactory.OpenSession()): H U. @$ {3 F% [" }
$ {5 T' ~+ x- d4 Y
//do transaction
- D* H: m5 U4 w( tgetCurrentSession().Submit();
* ]. V. W3 c0 H9 C! l" l# agetCurrentSession().RollBack();
/ t! c" j" A1 g9 v( e# [' E& I1 G...& N9 N1 x+ ~& @- M4 B- S1 e
Unbind(HttpContext.Current, SessionFactory)- S0 U+ R5 ?8 j( @
; Z* i! n8 I% J7 o [0 X
context.EndRequest += CloseSession;
) X, P- D; c0 Z0 n* X
+ ]' g' s+ t7 I- ~5 ^4 @ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|