 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 Y6 B5 X! `- B' L3 `基本参数:
& T0 T2 ]3 l0 P2 ajta | thread | managed | custom.Class
* v* y( S, C+ }' p% w5 ~主要是配合
, K; X# v" T6 s9 F7 j. VSessionFactory.getCurrentSession()& N! p5 x/ C& q
来用的.
2 s4 Q8 H/ N$ @4 |( u( aThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
! F7 H5 e. b |$ U$ gWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
' M1 w: o3 H+ U' d Ycontext.BeginRequest += OpenSession;* p" P' v# x* ^% y- ^; g
.Bind(HttpContext.Current, SessionFactory.OpenSession())# C$ m! o# b6 k# S* y S8 x1 R$ ]
4 @- T- s3 i( G
//do transaction- Z( x5 u5 j) b, d; V+ z9 Z; {
getCurrentSession().Submit();
- j* p/ K/ F; }getCurrentSession().RollBack();0 O: y2 E% X7 D* `! I. } I; a& G5 U
...4 Y% I, ?4 P* a3 q* _5 J0 _
Unbind(HttpContext.Current, SessionFactory)1 h4 |- S _3 h! K% F
* l" r) j) Y$ y1 t9 \% n
context.EndRequest += CloseSession;
3 s4 m6 N9 d0 p( G( h. m; U* u' C5 f3 S/ E" D
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|