 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
6 q$ o0 Z& [6 t) z. z! ?基本参数:
6 o. ^% Q( K; Q3 U- n+ L! T/ F7 n, ajta | thread | managed | custom.Class
$ e0 h- P- Q1 P9 b* R; {主要是配合
4 Q: x: d0 e* DSessionFactory.getCurrentSession()
) S$ b/ a- l$ N2 d, W+ D7 \- }2 f来用的.9 W, \, f( j" \1 }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 ] K. ]9 j5 X. Q# GWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:0 h/ }6 x' G! X3 A4 v4 I
context.BeginRequest += OpenSession;6 @3 V( q- l- F/ v1 f
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& l" L) W7 _+ F' L/ F' C2 u; u$ l$ o( S4 ^; [
//do transaction
& F4 H0 F/ z+ ~( \: agetCurrentSession().Submit();) F9 j% Q9 l1 }- U6 ^6 a8 K9 R1 ~
getCurrentSession().RollBack();
+ \7 x0 V2 M3 w, n...% I( u. r+ _5 }& |; f
Unbind(HttpContext.Current, SessionFactory)9 L# @3 R- o& e3 K5 o
. ~3 T* O8 D( t9 S; M
context.EndRequest += CloseSession;
( B" ?/ w% @# X2 b, r2 w, O& ]6 s- \* ~/ t, H% r
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|