 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 Z. {: t. L* P% e6 S. J/ d, H
基本参数:" z% r) Q+ H) d
jta | thread | managed | custom.Class
% Y( {! L* ^2 i+ I6 i主要是配合
' x2 N1 c- I# r k, ^SessionFactory.getCurrentSession()
j0 }1 r" ^' M2 g& [3 k3 [' M: T来用的.
7 K+ I7 E( ~8 s/ [8 A1 x. ?; yThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
. j$ u+ c# K% CWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
' L. o; I% Z R; f0 M+ b, ^context.BeginRequest += OpenSession;
1 j, ^6 N! J9 }' R8 ?.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 j% M; l# U3 S! o9 b. X
1 a( V& h' b' W; g( q//do transaction4 O* P7 B$ l5 ~( B# e6 y- s
getCurrentSession().Submit(); I( K1 }- Z9 ]2 C% V
getCurrentSession().RollBack();3 R; n. h5 @2 } B6 `$ ?" t
...& p0 @8 ?- o/ T) C" U/ E
Unbind(HttpContext.Current, SessionFactory)4 Q8 A/ z! S+ ^0 X3 t& B
* j* d7 ~) p B7 |
context.EndRequest += CloseSession;
9 z$ {8 I$ n }5 d1 O- k( D
* F8 j! q7 O. B% d$ ], B( H 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|