 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what* J' y" j& y0 N# P$ Z) e" l9 N" [
基本参数:
# i4 J* T0 E. u- j* Kjta | thread | managed | custom.Class8 A$ J$ D4 f, I7 S0 S+ h3 @
主要是配合8 M7 Y& c, Y6 y2 F) \5 Q d y
SessionFactory.getCurrentSession() G! x X0 ]) _7 v3 o2 p0 i4 n2 k
来用的.9 Q' o( I$ R; g* x3 z5 `0 ^
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
# I( z* j4 I, U9 G0 g) ~Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: h+ t% Z: \1 L ucontext.BeginRequest += OpenSession;
; ]+ j" p7 B3 @8 R% Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
* K& j+ J1 |, ]0 F8 U$ O. V+ D( H" ]' N2 E; H2 A
//do transaction: q3 Y. g5 y. e% i! b
getCurrentSession().Submit();) ^% v6 r K4 C1 Q: n& H3 ` O2 b. \
getCurrentSession().RollBack();
$ X( t/ B i6 N...8 h5 j4 d' } G
Unbind(HttpContext.Current, SessionFactory)% f' y, D0 N1 B' L+ T+ W
& e5 o1 U/ u" Q: Y/ }0 \: @context.EndRequest += CloseSession;: _& a6 i2 v( F0 ]; R2 V' D
- J' w B0 `% `: O" O" S2 H
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|