 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ n! w9 }3 O( k! l, ^3 T; A基本参数:5 `4 p) T" g# y K8 ^7 W
jta | thread | managed | custom.Class
4 W. M' ]; {+ H$ G主要是配合
, T4 ]5 p) k/ jSessionFactory.getCurrentSession()) X4 a4 I4 i! X
来用的.9 V' K& u$ F) C( F F- C
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,& y; t' R6 v6 D, D% ^
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:4 L. P0 |# O+ _' [/ a7 N
context.BeginRequest += OpenSession;' @& a9 F5 _+ ?7 `2 ^0 ]4 i
.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 ^6 ^4 Y- V( B. X; p
( G" C. r% ~( L |! J//do transaction
3 e/ B3 l- v$ @/ F' M- ?getCurrentSession().Submit();4 q( A4 O9 {6 e+ i3 l
getCurrentSession().RollBack();
$ @3 ~" P) |7 k- m: ~6 R( x...
0 P8 L3 u/ x3 e4 J+ \6 [: kUnbind(HttpContext.Current, SessionFactory)
4 @- I1 P: F# }: ^3 t9 a4 X) x( a% ]: w8 g1 y8 _1 S% S
context.EndRequest += CloseSession;
' A# G' k# b4 L* C' w
% B: n8 A2 U( p( I 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|