 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
3 m! _9 G( \9 V, ]8 | x0 v基本参数:
4 w' {5 U% B4 U" Cjta | thread | managed | custom.Class
6 Z& H4 j% w% O/ R( S主要是配合
& j g+ d1 H2 j2 J1 c" c+ x. u5 nSessionFactory.getCurrentSession(): ^1 c. a2 O0 ^ E# k
来用的.
3 h7 s: e8 T! Z* F9 C' ?1 ?2 Z6 \Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- K% ^: G' V6 k" W H
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 d( n7 e4 G! Z8 M6 x8 @context.BeginRequest += OpenSession;
4 A- @6 f1 A# h: ~/ |.Bind(HttpContext.Current, SessionFactory.OpenSession())
! }5 `2 z% y5 f I; J# U4 T' z, Z* j
//do transaction# H: H7 F' U9 K! m6 g5 u
getCurrentSession().Submit(); n! m6 q! h, { e2 _% f& j
getCurrentSession().RollBack();
# v6 a) H4 e4 T' g) z$ j4 t1 h9 T- X...
* O' `" g2 }$ P! S* \( X/ rUnbind(HttpContext.Current, SessionFactory)
4 ~$ K4 n* P" [; h$ v: ~
3 U9 M9 Z% \* F% L) e! [4 {5 Fcontext.EndRequest += CloseSession;, ?4 g: K* U( s* [
' c& P; v; }$ B) y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|