 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
5 T% S8 c- g9 l( q基本参数:
( z4 S" u- E e. Xjta | thread | managed | custom.Class
( Q x$ P w, @5 i) `, p, [主要是配合
# X3 p5 Z* w9 S( JSessionFactory.getCurrentSession()
% |. }9 q& \8 |, E8 C( _* }8 r来用的.
3 J3 R9 B* Y: N: v6 ~Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 h5 f; c2 n8 |1 YWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
/ v) a# M# s0 l* c2 k/ Econtext.BeginRequest += OpenSession;
8 |6 q/ A4 `8 }# o; Z0 e.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 G! j; e$ _5 T8 h3 D9 {8 ]$ J1 W0 @# T0 I3 A
//do transaction( P5 W# t) C% G9 \3 s1 k" M
getCurrentSession().Submit();
, e. {, T: q8 r F4 L1 u$ fgetCurrentSession().RollBack();9 \6 F5 ^/ _; [* Y+ K: C
...
. u* r% a! ^- b" r- Y# \, u5 Y/ J" QUnbind(HttpContext.Current, SessionFactory)
$ m' a& t, `4 a# N; I
' ~% A% s; b0 l% [2 Q4 ycontext.EndRequest += CloseSession;8 n9 x7 `$ n: x& y2 h8 w$ d
7 @2 X* M! E7 `: Q1 {0 h 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|