 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what f" E/ d, i3 Y/ ]% F' k
基本参数:6 |4 o( R# `3 i$ G' e
jta | thread | managed | custom.Class4 y' L# P! m% V! G
主要是配合
1 P9 {; |3 ^' g+ J6 n8 |SessionFactory.getCurrentSession()
% y$ |, e/ O: ~) C来用的.
* _8 G; Y1 P: NThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,# ~) B8 u+ Y! r: |
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, |# t" O' O- ?1 J1 `( W
context.BeginRequest += OpenSession;
0 l2 I+ o N' k: I- i$ e" L2 g.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 O6 d i' H% j. ?- O' [/ R/ a3 {- } }6 y( d
//do transaction
( t0 A) X4 t" j$ rgetCurrentSession().Submit();
: d6 o1 p `4 U2 SgetCurrentSession().RollBack();
! P- q0 w" |' l. P A+ V6 G0 U...
0 z) L( k j9 V# IUnbind(HttpContext.Current, SessionFactory) `% M5 i- {# g& Y+ Y; {
" S$ ?& s! Z o2 y: K! \context.EndRequest += CloseSession;% h. o! V0 X1 _9 g& B3 z
% e& X' I5 W/ o$ ` 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|