 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
( g7 s. g" F* p% X基本参数:
# u) k3 d, {1 L3 O6 |, _5 A& |jta | thread | managed | custom.Class
2 f; K+ Q+ h y+ w2 x主要是配合
/ S- R+ {3 K' W7 B, P! Z1 n! GSessionFactory.getCurrentSession(); v, |$ G! ~/ ^6 c7 y2 y
来用的.
* ^1 q' ]3 x8 M' M+ m) D3 pThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,* \8 n& B) C& v0 O$ \- w
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
2 s, V$ V: ]0 ^/ O* M2 F( n8 bcontext.BeginRequest += OpenSession;* D4 b2 g4 q4 l$ ]
.Bind(HttpContext.Current, SessionFactory.OpenSession()); i% [1 [1 `' R+ j
: f: E6 t9 k# ?5 Z
//do transaction. O% C4 t& t& U3 e7 F
getCurrentSession().Submit();
# U+ \1 x0 z2 VgetCurrentSession().RollBack();
8 M5 W- m0 L5 s a6 E6 p! G...
9 C ^, |2 z! J& T5 ], `% pUnbind(HttpContext.Current, SessionFactory)
% s& c. f! k+ a2 r: j+ b" `5 Z8 P; o) m8 Z& D' B1 p: t
context.EndRequest += CloseSession;
. C9 `5 v O: L V* f0 X9 y) i4 w. k6 P# g B) y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|