 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ E- a' c4 { |. k
基本参数:9 n. s* I0 U5 z: B) ?2 Z3 |
jta | thread | managed | custom.Class
* @* w6 Z" M! y5 d. F% g主要是配合
/ H) f J( T: z, x+ MSessionFactory.getCurrentSession()
- h6 U- s* o& o" S5 {4 j) M4 i来用的.
9 H; g+ w* m; D$ ]& A7 O% qThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
, A; `* C' B) o( @. y5 m" MWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: P. M* d! w% F; J# G. B' z
context.BeginRequest += OpenSession;
% D3 W- q3 |5 w6 M- c) }.Bind(HttpContext.Current, SessionFactory.OpenSession())* H$ E8 x; ]! {$ e3 ]+ ~: M
0 ^( d& A$ |% t6 S//do transaction0 t. G( b3 O& j7 E+ O
getCurrentSession().Submit();; o5 b0 E+ o7 f" `( g
getCurrentSession().RollBack();1 A; e% p" ~$ i- D: Q
...# G/ J" {& I' G1 r4 @8 c: ~
Unbind(HttpContext.Current, SessionFactory)
7 `6 m! }! L4 q9 z1 X* k
6 h" }; f1 w" h* ^' K% |: O" qcontext.EndRequest += CloseSession;# P' {0 s' {, a7 q
% ~8 P7 _8 T7 G4 C4 X% L b
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|