 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% r5 I6 G- q! n, D% _
基本参数:8 J( \) y4 I8 p5 L. D
jta | thread | managed | custom.Class
4 @# t6 n1 x8 S/ c+ C b主要是配合
7 |$ f# u; X3 {8 `- B# USessionFactory.getCurrentSession()
- a: y6 a6 \. i+ a% n+ u来用的.8 @2 @ \* M8 e, ]. h
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ R- S2 `1 n# fWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# g; C% x7 Q# econtext.BeginRequest += OpenSession;, o' f: J9 f) U% A3 w( \! `: F
.Bind(HttpContext.Current, SessionFactory.OpenSession())" Z( _" v/ y0 y: J$ N4 _0 R @5 q4 E
& @1 u6 p' M- n5 u- ~//do transaction) T0 {: ?$ B; o" d, Z$ Z
getCurrentSession().Submit();; @9 L* W7 x; U
getCurrentSession().RollBack();4 y+ E( f. `0 c; |0 }4 X2 i+ y
...
9 p. W9 k, Z% r8 V5 z9 a7 a0 i. BUnbind(HttpContext.Current, SessionFactory)
% U0 K d; o8 E' N3 C W
% E& F ^" e$ t0 u& Econtext.EndRequest += CloseSession;
* _9 m9 I2 [, c5 i6 B0 G/ u( s. P! A( D% M% c$ v# d9 `0 }
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|