 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what+ B ?- }& b4 i2 S+ t
基本参数:, t( V3 S# b+ h; C
jta | thread | managed | custom.Class) S* w) [6 Z$ Q" l& O& s0 Q3 m; i
主要是配合
$ F/ M+ x1 s! a' ]: _ oSessionFactory.getCurrentSession()
* l9 Q& [/ D3 F3 T3 e4 Y来用的.
7 e2 T& b( f, A, e5 w9 S* R& wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% K2 ]3 D1 u' t+ u% r
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:: g3 ~* ~: `; N# l% j7 a# i& ]! M
context.BeginRequest += OpenSession;
5 S2 y6 E0 @2 H: [.Bind(HttpContext.Current, SessionFactory.OpenSession()); m6 J5 T; D* m# ^5 P" [
# p* _. R( L3 S1 W; L- x
//do transaction
2 \9 y. J9 n/ O6 egetCurrentSession().Submit();- E3 o3 Q& Y" M; \/ I y# E
getCurrentSession().RollBack();8 P& J& _) A9 Y1 l' G: {
...3 }% v4 V! L7 n- s. }7 w
Unbind(HttpContext.Current, SessionFactory)
4 y6 J: ~8 u& ~8 s0 z+ \9 W6 I9 g, c7 _& z
context.EndRequest += CloseSession; e8 J9 Y1 p9 Y- O# H8 ^! b
- G* b0 `4 E+ D8 Y& X0 U" w0 z& P
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|