 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ i' i5 B: i+ |9 u! E/ B
基本参数:, f) M. H. l' a3 q
jta | thread | managed | custom.Class
/ i3 g4 T5 f# v主要是配合) B: u0 N& @4 X, G$ {: c
SessionFactory.getCurrentSession()
6 |& o( ^8 G1 j& }# ]/ b来用的.# ^( i y$ ?& x) G9 o* u
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
' N. T# h7 z6 y% l. _& h U7 p9 RWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- |' n; i1 `7 ycontext.BeginRequest += OpenSession;
% N4 M7 o% Q7 D. L0 X6 h.Bind(HttpContext.Current, SessionFactory.OpenSession())5 _1 m5 Z1 y, z( s6 j- l$ E
+ K# Y6 Z& q; `3 S: _//do transaction \8 \0 ~# `5 n% z2 c
getCurrentSession().Submit();
4 Y y* q7 U: B5 }+ GgetCurrentSession().RollBack();
: ^3 ] H4 w" @... D; w+ ^" @- s$ j! m/ X) ] g
Unbind(HttpContext.Current, SessionFactory)
4 i7 y F9 ~3 x% Q4 e9 f$ F8 `; y5 g* N' T* Z* s
context.EndRequest += CloseSession;5 `) a, \! `$ E0 F" w' a
6 x/ b# M6 J2 u0 ^ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|