 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
, y$ W6 {8 I' {1 f: `/ T+ M基本参数:
% m' T, D1 u/ sjta | thread | managed | custom.Class
* a' Y9 \* F* _0 k: R主要是配合
6 _5 C+ _9 U7 K1 w7 v9 aSessionFactory.getCurrentSession()9 W* A* c T; P1 W2 W6 ?
来用的.$ P1 m! S% P* k; ?6 O; D# C8 m
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! U1 x0 d8 R7 j* x& X" s
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; i! a& ?. ~; A; u3 Y
context.BeginRequest += OpenSession;. ]; j5 ~6 S* t4 X7 T
.Bind(HttpContext.Current, SessionFactory.OpenSession())
+ S- a/ j' {0 b+ F' Z" Q1 X8 c" v" M; B
//do transaction5 m9 ]) \; a5 H/ ]4 k: J6 Z1 }
getCurrentSession().Submit();- { Y5 O& ^2 k/ a, H
getCurrentSession().RollBack();& U9 o- d4 A- m
...
6 e2 V: f& V+ c7 a" N1 xUnbind(HttpContext.Current, SessionFactory)4 l8 x, \, E# e2 m! s4 I4 G
+ R d" ^' \/ o9 [' o6 U* Tcontext.EndRequest += CloseSession;
" M9 F5 Q4 m D5 _- g" [+ ]% B- a/ s1 l) {: @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|