 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 d G8 o3 W- L
基本参数:
7 {) ]- |7 b/ T1 s- t; j' fjta | thread | managed | custom.Class) |# R+ w; Z0 O7 C6 O
主要是配合
( O/ ]# u8 A" p% P) [SessionFactory.getCurrentSession()
8 V$ R& S5 m- w来用的.6 J$ y. ]) L# j/ O2 Q2 w
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,( }( O. \" ~+ {; W
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 o8 [/ m' ^# [! k* \9 M% e' y
context.BeginRequest += OpenSession;& z" q, Q! s3 M+ J" `; T @
.Bind(HttpContext.Current, SessionFactory.OpenSession())
% t* c) |0 }4 _. @
* d1 Z" J0 @3 ]! [, }//do transaction
$ b2 M- k- Z# j, h% `4 ^9 PgetCurrentSession().Submit();
. _. G+ v# ^, r) b! A+ B$ q3 T$ c( J- DgetCurrentSession().RollBack();
/ l! W/ q; Q P* T- X...2 I% x2 V* a5 m! I2 E
Unbind(HttpContext.Current, SessionFactory)- V( v/ B/ O" V, [7 E/ n0 e& H2 s
' K+ S% f- k* v! O mcontext.EndRequest += CloseSession;
$ E. u% B7 Z: i& }7 W; ^" U- O: n
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|