 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% H& `$ x3 ~. S# M
基本参数:
0 l9 t' B2 Z+ T( Djta | thread | managed | custom.Class
5 ^8 G k, U- ?& d% D- O主要是配合5 e, Y& z( |" J6 N
SessionFactory.getCurrentSession()0 C* G9 m( N$ W
来用的.
6 }2 h* [6 J7 }Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: k' `+ l7 ~( t
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ @+ h1 O$ e. ?3 n D$ Dcontext.BeginRequest += OpenSession;
- I+ k. c/ l+ H/ `.Bind(HttpContext.Current, SessionFactory.OpenSession())$ Y) ]' X8 Y0 S5 ~8 }
5 a& x1 N' v. [/ o) V
//do transaction( Y) d, {( T( @4 R/ x
getCurrentSession().Submit();
( U* V3 y" F" J6 h( ]' Q9 ~7 t; UgetCurrentSession().RollBack();
7 v2 Y& ] A1 h! L...7 r) G9 ?2 ?7 Q2 e
Unbind(HttpContext.Current, SessionFactory)
' U( n+ s0 @' m5 C, j8 e' d: @+ K+ X4 n3 [8 G l
context.EndRequest += CloseSession;" F- v# A" [" c, b$ E* F4 O" }
6 _2 i9 S+ I$ B2 Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|