 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what/ V+ e. ]/ O! R7 E* G
基本参数:
5 p5 d+ b9 c+ @7 q( p* l' [jta | thread | managed | custom.Class( W1 C7 g7 P; k' I) @/ K a* Z
主要是配合& j8 N. C* [! T% K: y7 L8 f+ i
SessionFactory.getCurrentSession()
+ o+ c6 H: w* W来用的.9 i) X8 B1 @# m+ B- m. K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,5 t S7 S1 I' a# E( m& F4 E
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% V: u6 t- A% r8 m
context.BeginRequest += OpenSession;2 z! C1 N# I. Z) y' ~7 _
.Bind(HttpContext.Current, SessionFactory.OpenSession())$ i0 O1 Z: a0 B' B5 ]
" Y5 h* h. U& z9 e7 ]5 _//do transaction
7 @) l2 u7 I) ~4 u3 w- p6 R- WgetCurrentSession().Submit();' D& [) v# P; i' I7 \* [
getCurrentSession().RollBack();
. _# ~* [9 @2 e4 i- \. ]5 ?...9 ?* z7 q; U: T7 y* w8 W
Unbind(HttpContext.Current, SessionFactory)
+ U# f6 @, e% p1 @' X7 s# T) ~! X: B, k0 T: o% @* n H5 ^. `
context.EndRequest += CloseSession;
7 Q& D D, @" q- ?" W
6 F% R4 n$ a0 x1 J5 I4 N 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|