 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' s6 Y& D6 Y, e4 `; R, k6 H A
基本参数:
% P+ z, y) {8 [! _: wjta | thread | managed | custom.Class
2 |( X6 S# m. i6 p主要是配合0 T5 a! e! p' b: I g9 @$ r
SessionFactory.getCurrentSession()* n9 l% W- ]. M% }3 r, z/ s" ~2 N: c
来用的.; t$ Q$ `: \" K% [$ L/ p- N- i) @& u
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: t/ C' A" @& z [. Q
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 Y7 c6 x) d1 {1 F% scontext.BeginRequest += OpenSession;9 w* T" f" o6 P
.Bind(HttpContext.Current, SessionFactory.OpenSession())" r p0 W; L2 |; N' x
: Z' D% e/ k( Z# w5 t6 \
//do transaction4 H y4 U9 I4 I2 w1 ]
getCurrentSession().Submit();
- r, z8 j) s0 n! r7 ?6 j6 ygetCurrentSession().RollBack();4 f* n2 a D+ X) A- o6 B& J$ j1 _
...
& k' t @' \8 g: B" U B9 [7 ZUnbind(HttpContext.Current, SessionFactory) [' t& ~8 M6 ]' y" x
; y; [: r, V; t" e: ~' p, b% a$ K5 wcontext.EndRequest += CloseSession;
. Y3 B1 ?$ x; J; D$ I Y% l. [# h. g" n' \/ ?7 \; `" @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|