 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! a8 x7 v# R: ?" D: s1 d% V8 \* H基本参数:
$ Z/ u5 s' j/ T, P2 X8 Wjta | thread | managed | custom.Class
x% p7 p; ?' x/ r, {8 H主要是配合
7 J. m5 ~8 C: mSessionFactory.getCurrentSession()! a& r. e: p* C) S7 ^) I
来用的.
6 {. A1 p4 m6 f; I+ AThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' C: @9 V$ I( S- o* V
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 e( ~8 L! h! }5 H/ U
context.BeginRequest += OpenSession;
, h0 l, [( S3 x9 X. D: a/ Z.Bind(HttpContext.Current, SessionFactory.OpenSession())0 _* C$ d C# y' @$ n/ @' f
5 c( T$ w4 A" @1 Z3 x
//do transaction. s0 S6 }" E. ^: W+ @
getCurrentSession().Submit();
& a. z1 o6 ?" |9 I8 B2 }4 n7 Z4 ngetCurrentSession().RollBack();+ c* z; g+ ^' s, v; _
...
* \$ G! V; N& B6 GUnbind(HttpContext.Current, SessionFactory)
! b' C# g/ V- F" q2 m8 A8 t7 O
+ g1 Z* L) C& _+ L+ [0 ycontext.EndRequest += CloseSession;
# {, V% m8 ^; s
$ w. g, o3 m, D 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|