 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
5 f; j# O- P( u1 u! E$ s基本参数:
6 I4 @6 D4 M i/ B" f1 J) njta | thread | managed | custom.Class
: R# g8 ?2 m) w) i5 p" i主要是配合; Y) W% I: `, [" W& z9 S9 ]; d1 a, T
SessionFactory.getCurrentSession()5 k1 i* {) M7 y) c7 o$ g. ]
来用的.
V5 [# c& [/ t) h. q3 p0 T! f" yThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 }6 X) V7 P3 q XWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
4 X( i6 h$ ?6 T$ W. ~0 p2 _( Mcontext.BeginRequest += OpenSession;
; `; L5 v& K; W8 ^4 {.Bind(HttpContext.Current, SessionFactory.OpenSession())
7 `# @' Y+ ^1 l1 w1 \% o
+ }6 D8 g6 S6 j1 T//do transaction7 q8 W" W. f) R0 n
getCurrentSession().Submit();
# O( V8 Z& M U; o# YgetCurrentSession().RollBack();
x; K' @. l* m ?...; k5 a4 k8 _- R" T% z* l, v9 d @
Unbind(HttpContext.Current, SessionFactory)0 i C: [6 f, {3 X- O" Q
! V) r% F2 J. Z) L2 A2 Acontext.EndRequest += CloseSession;
. a/ r& l9 X. C2 X1 Z
6 C, l* P" e# W4 k y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|