 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. `/ S& a9 L$ m* G6 C基本参数:
8 q0 Y( P* D/ \ I- ]jta | thread | managed | custom.Class5 C( |0 d: x6 u4 g1 ], D* L$ A
主要是配合3 ?. I/ @1 z% j% h8 k
SessionFactory.getCurrentSession()
4 f2 [5 U5 ?, f9 O( K6 i2 r1 J来用的.( k1 _9 }5 P( z j+ _( Q4 |
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
1 Q o5 D0 _0 \! p% x8 pWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; t& j* e6 b+ z9 ]1 q0 K# o: u, e
context.BeginRequest += OpenSession;7 `, W$ q j$ \0 I/ e1 v6 ^+ Y) C
.Bind(HttpContext.Current, SessionFactory.OpenSession())9 Q- `$ {1 f& s3 K+ b
0 w: {3 u% H; i9 e5 ~) s//do transaction
- y/ q8 V1 U0 R& ^% O, pgetCurrentSession().Submit();' p6 m: u9 P$ \" `* n ]/ c# N
getCurrentSession().RollBack();1 `# [8 `5 [* E" C
...
$ k! {7 J$ E# MUnbind(HttpContext.Current, SessionFactory)
% X" |1 z! g$ }- N" Q: K- w9 }( w
context.EndRequest += CloseSession;
6 r$ z( V: j3 K0 x1 O6 W/ ?) ]+ ?5 d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|