 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: T2 K3 r4 _2 ?' ^0 X5 }
基本参数:
" _ `/ Y9 a( x6 D# i" `8 v5 K2 W4 m/ Qjta | thread | managed | custom.Class* Z `- w' E8 H; T
主要是配合- w4 |) m) ^9 f0 U7 y2 u7 W
SessionFactory.getCurrentSession()
/ w& V$ k$ K* x2 G来用的.) }8 g C; M" |6 a
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 [0 G* w3 C- g$ C; B
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
/ u2 I9 j; P5 `3 wcontext.BeginRequest += OpenSession;
2 B1 C. \- s1 D# @ ^7 Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
) [7 S7 `( j- {7 V9 I9 Q) o
5 v( P6 `3 p0 n. ? R" Q//do transaction* u o+ b# P3 H; P
getCurrentSession().Submit();
$ X* g, g4 h) d2 c% ogetCurrentSession().RollBack();
9 [9 ]' k$ s; X7 m...
, E, s6 `& {4 X+ x5 h, P& QUnbind(HttpContext.Current, SessionFactory)
: ~0 g4 |6 x; v/ v( Y5 K
/ k! p! d9 ]7 gcontext.EndRequest += CloseSession;
1 K; }. G Q' ~1 [; j
( Z9 P0 C9 ]/ g" W8 q, G4 q 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|