 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: [* L; W! T) J- y8 c7 a3 w7 Y$ H }
基本参数:/ M/ D- v8 m& d& \) B3 j4 G; R
jta | thread | managed | custom.Class
' M- J, Q# }5 m9 b$ y5 K主要是配合
8 p& K# m4 `: [) M C+ h/ f+ u$ W8 JSessionFactory.getCurrentSession()
2 v6 {2 B) a6 B/ t4 E3 v3 K来用的.
# y( V0 s) n# g/ k0 y# JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ R; u. v8 U8 X: E
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:! T: x- [7 B- ^. m" }% s! x1 s
context.BeginRequest += OpenSession;
: p- ]: M6 Q* a$ Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 D4 |, a7 o5 G$ [7 J" V1 c& g1 z5 g' s* v! r" h& t6 Y
//do transaction9 N; l+ d; \; z: r' M+ O5 m
getCurrentSession().Submit();# }& j/ M; G0 v5 R9 H, ~4 d: z
getCurrentSession().RollBack();, J7 e. @# d1 K# i% P
...& g) _0 {4 m8 |& J7 W
Unbind(HttpContext.Current, SessionFactory)* m4 j* `/ [; j2 h d
9 r: {) l; F& D# x4 l/ z) b' z; Tcontext.EndRequest += CloseSession;
+ i3 ~+ ^& y5 V, H
6 ^$ I. D# |+ u& G 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|