 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ |! Z9 L$ ^7 D, c$ C2 o
基本参数: p# o' o w- B& `: A/ J8 e+ s
jta | thread | managed | custom.Class
5 A% S1 [( E' h3 q; G* X i主要是配合8 {9 @0 F( @1 I9 ~+ L+ D7 @, V7 i+ U
SessionFactory.getCurrentSession()' m1 P1 h: X' @4 z
来用的.
+ D8 `& l8 g% y+ ]# I( e$ I. GThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 H5 f: V( X. y4 r9 ?Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( c5 ~( t+ s" E4 ^/ Ocontext.BeginRequest += OpenSession;
: g* Y1 `7 C$ s- l/ N.Bind(HttpContext.Current, SessionFactory.OpenSession())) j9 _, [- d. a9 f6 A5 q6 i
0 H7 k0 L+ @ D) s( @+ U. E
//do transaction
0 p+ k% b& a" ^; LgetCurrentSession().Submit();
$ Y/ G; I) I7 j! u6 OgetCurrentSession().RollBack();7 |# T. ?# B o, J* B& t3 {7 A
...
( |$ ^# l5 u$ l/ I9 a% d7 Y6 m4 }" d6 IUnbind(HttpContext.Current, SessionFactory). D* p8 \+ b( k U
s4 ~5 ?6 u/ I2 C" L' U' F
context.EndRequest += CloseSession;# D" s @8 T& M# v5 |
0 o4 C- a- K) s/ |
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|