 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what" m5 K3 G& J) s5 |3 j
基本参数:
) O% n9 ^2 f- |! b/ ^4 P5 @# k" Ejta | thread | managed | custom.Class
. c- J% x5 P4 N6 P- Q; e主要是配合 O- B- T- C& v/ m! u) o q2 _
SessionFactory.getCurrentSession()7 M# j* p; J d( M( Y" U+ O
来用的.% F6 {% E* \; K1 `0 K* P2 E
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: B9 E0 K; F6 Z9 Z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 } i- W9 v! z7 x4 L
context.BeginRequest += OpenSession;
( a, T6 V$ ]! H1 B4 @.Bind(HttpContext.Current, SessionFactory.OpenSession())( ]/ K: D, Y I5 b# I
) k* k8 Y# [& M0 T2 b+ e( S* Z//do transaction
' X4 h( |' B! W3 @% }getCurrentSession().Submit();
2 F0 j4 `# [ b! W' f+ h5 ZgetCurrentSession().RollBack();
% I- \4 ~/ c" O. S* f/ R% o...
! i9 N& @0 o$ T: TUnbind(HttpContext.Current, SessionFactory)
- Q, @" B1 [) W" A# P: I: i+ M; _2 J& F0 E C
context.EndRequest += CloseSession;8 {* G3 K* I4 \" B, |- g$ _6 L
( v6 M: b" O; t& ]) {4 R& I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|