 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 ^ A! s2 e( P" G, N7 C
基本参数:* o& k+ |! n! m; g/ I( F3 J: q1 N9 B
jta | thread | managed | custom.Class
) o: Z' m6 ^( o7 L主要是配合
, f" L. b4 O& e8 d5 h( \/ i$ rSessionFactory.getCurrentSession()
" ` g+ S' n( [1 w7 T |* T5 C! p7 h来用的. k2 U* f% ]7 P
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; L4 X: b& O# c" J/ CWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:4 i @- }8 J2 W2 [5 g
context.BeginRequest += OpenSession;
. M, C/ {, T' j.Bind(HttpContext.Current, SessionFactory.OpenSession())
: I9 |6 } Y7 @8 C9 N
3 q' O8 I$ M( E: I2 d& o//do transaction4 @+ i& v+ r0 c2 c$ n' |" c
getCurrentSession().Submit();
0 z+ z7 H, b' Q* ]getCurrentSession().RollBack();
4 ?3 O4 A9 H; k1 }- G7 c% r- h...
7 Q$ \9 T6 H6 L0 k4 dUnbind(HttpContext.Current, SessionFactory)& c1 l: R$ {/ U# F! R* ~) ?) j. H
- k. ~7 O7 Q% i" L4 A0 zcontext.EndRequest += CloseSession;0 s' {% t$ R, X6 q- K
4 n2 p# x7 {, ]0 d- w, d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|