 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 d% { ~/ [) I* j" o% Y
基本参数:! Y. |8 n$ G2 p5 M" e h6 Q5 r
jta | thread | managed | custom.Class
- d! ]. K) l4 Y9 {" k主要是配合. f! n: R4 y' O& s" ^
SessionFactory.getCurrentSession()
9 ?( D5 E0 A* F0 C来用的.
) M0 r. m* i( b; Z' Q; P+ }0 VThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: U' Y/ G8 F6 p8 a: i. p
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! b8 r+ X. b3 W7 Z* q, W( Scontext.BeginRequest += OpenSession;
+ B+ D) C. \- \7 j- P- @.Bind(HttpContext.Current, SessionFactory.OpenSession())/ ]2 x4 N! c9 k2 ^2 c1 O/ m; |4 l
) [; b4 A [' v# _& P//do transaction) J' B3 D) g2 [7 a( U+ c
getCurrentSession().Submit();
1 d9 q3 [" |% p! `0 u+ HgetCurrentSession().RollBack();
6 n4 F, W( [3 X5 f2 R4 Y+ R: M..., d7 n ^: ?6 L$ `
Unbind(HttpContext.Current, SessionFactory)
; T5 |4 |; k' ]/ v9 E. p: K! q5 K; O, Z8 b/ f' `* C
context.EndRequest += CloseSession;) t- j/ Z. S' v
$ t: Q4 o- v* r N
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|