 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 _" z- G3 n* y4 d5 I基本参数:
$ ?4 P' h- W/ v7 F: |jta | thread | managed | custom.Class
! K3 S. n9 Z; b7 V: ~- D7 N3 S主要是配合8 s6 O8 `! G! h. i
SessionFactory.getCurrentSession()! I' A$ P( O1 Y6 I! Z
来用的.7 c/ \! l+ e) Y) n* B: V( Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) e$ V2 k" }" i1 j8 R1 J/ cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 t4 X! W# _: ~* g: A( w
context.BeginRequest += OpenSession;5 I1 x& z0 N. i# f2 H# v
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& f) v& j+ K0 r
& j N6 n# p' M; e( ^4 a//do transaction
# h/ V; Z, _( o- {- N4 kgetCurrentSession().Submit();
6 H$ p; ^- k+ I5 }' d) }9 E" {getCurrentSession().RollBack();# i3 d9 V8 \. s: `, K
...( S* E- {* X! L/ x9 h) z! T y
Unbind(HttpContext.Current, SessionFactory)0 X! \2 {3 Q8 i( g$ W
6 `9 k% Z3 n* Z. acontext.EndRequest += CloseSession;
- f4 ^: D: w. x; ^! L$ V7 X' c% @$ B. R; f- B
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|