 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! v, U K4 A' X8 \# L
基本参数:: {: ?2 u: V' H6 V1 w8 n$ S+ _
jta | thread | managed | custom.Class
/ P ~2 U3 w: I, M主要是配合: E9 d1 {+ d V# ~" _" B9 y
SessionFactory.getCurrentSession()
; v8 p( c1 m; X+ a; `3 A来用的.$ ~. r) a: t( v
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,4 d, S+ E( d, @8 N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
. w, W- d/ s Q4 Y' ~- h6 lcontext.BeginRequest += OpenSession;; w- G) G3 A+ Z+ V
.Bind(HttpContext.Current, SessionFactory.OpenSession())
: w1 H& l& P' A# X4 s
% x1 m1 z1 P5 t. q- J/ }) M4 |' e//do transaction$ O. [" X% @# t. R! J
getCurrentSession().Submit();
& k8 X9 O; ]' V8 ggetCurrentSession().RollBack();/ N! d. U, S, S- V. t P
...
9 H' z5 H$ _' J0 n, ~$ H( qUnbind(HttpContext.Current, SessionFactory)
8 P) h- T* z2 o, n& ^9 x ]/ {3 @& W# r' n
context.EndRequest += CloseSession;
9 w0 |# `2 \: m q( y0 R2 z
4 l; W" X9 l1 A6 } 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|