 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) c4 p5 _9 J0 z% s4 [
基本参数:
# K2 p! B" \7 ^# {1 o" f2 Rjta | thread | managed | custom.Class
, [! w4 \/ U8 f2 `主要是配合
$ C) e, K) M) j3 o! P9 `: i! WSessionFactory.getCurrentSession(), f! s8 g' G8 Y; j5 }
来用的. C9 ]# D- g% v2 A
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, S1 c+ u1 p* x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:" h% d+ C9 A+ b" c4 `: g$ S
context.BeginRequest += OpenSession;
# I( u. Z3 q% h* { L$ b# F.Bind(HttpContext.Current, SessionFactory.OpenSession())! k u/ ]0 S: h7 e) q$ M6 c
7 L$ t+ Y3 V5 T. L7 u//do transaction3 h5 o$ j( n% b. W
getCurrentSession().Submit();
& O. @$ I+ \" p0 G' z9 ]getCurrentSession().RollBack();! W5 D8 Y0 M& y/ @
..." Y8 W/ n" S) A2 z. a4 @
Unbind(HttpContext.Current, SessionFactory)" e$ s( m1 \1 A0 w7 b* Y1 M
" H, `& y6 [* b; y- R7 B
context.EndRequest += CloseSession;
' V: _# U- P7 @0 ]
n9 q" t) y2 q: v7 Z) K 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|