 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% S c" o4 N4 q4 t& F+ U$ d1 e# w4 x
基本参数:
, T; x/ K# w# `! ]3 ?. {jta | thread | managed | custom.Class5 m9 J. u8 x% k3 ^% q( G8 L
主要是配合
$ X8 E. D4 r7 z BSessionFactory.getCurrentSession()8 i A) {/ \! c' W
来用的.: `1 U* d- L7 a# ^0 {/ Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 M/ `. r5 T7 V" l2 V, X
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:5 r8 B% T. C4 N o2 b" y3 }8 A
context.BeginRequest += OpenSession;: m; E- P: u4 U: I' c& T1 q
.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ p0 h7 f& w0 T1 x8 q& H4 P9 n/ P9 @. y2 A/ T" M' r# j" l
//do transaction" c% P) ?* X3 q9 S/ E8 I
getCurrentSession().Submit();( C6 A1 E) H& j0 S' S
getCurrentSession().RollBack();
% f; l( I% Q$ L# v3 w9 e. G...& x3 V$ w; v0 |/ f7 h
Unbind(HttpContext.Current, SessionFactory); u2 F% N2 h- C2 F7 n
* @% Q R% `( [& o% |; n
context.EndRequest += CloseSession;1 L. M; i' Y2 c4 |! j
/ Z# M5 R- T* c+ L8 x! \$ ^1 U
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|