 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% @3 N1 t, Z! p0 Z7 {8 ]7 F3 n
基本参数:
5 g' ]/ }% Z( U0 J* q+ Jjta | thread | managed | custom.Class
1 y) j- h* \' P; Q! {' C1 O主要是配合
+ U: R/ n. h8 Z9 P0 K& nSessionFactory.getCurrentSession()
' ^ Q/ J: u' y/ G0 S5 w来用的.
, \! h) O1 Q1 w5 JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- V1 V, X3 U2 { p0 q1 g. L' NWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 |* D! ~1 R/ l4 ~$ zcontext.BeginRequest += OpenSession;
' G! k! m/ x( X, z, H3 ~.Bind(HttpContext.Current, SessionFactory.OpenSession())
, s; m5 k$ w& R, V- f/ S5 }; s3 _6 E+ r7 _( D- w$ \5 I, a. |$ d3 U
//do transaction
5 w* f# m* K7 F9 l+ h: N. ZgetCurrentSession().Submit();
x) Y, K- I6 ~( r* ~getCurrentSession().RollBack();! N9 Z" [ D J4 N0 {/ G
...9 S" W. U' F7 M! X f
Unbind(HttpContext.Current, SessionFactory)/ U( b* ^+ M5 l5 c% ]( l& n
/ S% H6 d5 C# E2 G" kcontext.EndRequest += CloseSession;
9 {+ C' _& J+ J. A. Z& W+ S s* W% E( d) \% W6 P
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|