 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: a2 c; s' D, G5 a/ _. C" i8 O# p
基本参数:
+ B+ E! @6 ]3 ^& j2 njta | thread | managed | custom.Class
" x7 Y& x" t4 I( c' _/ s主要是配合
* N, _% p0 a7 PSessionFactory.getCurrentSession()# X/ J& L6 ?; x: @8 Q: J* r4 m. I
来用的.. Q7 g& J! g+ P1 A% Q. I: F7 n2 x) C
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- l* }. D! D* c5 C
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
% U9 x. d5 x7 ^: e) D& i6 ucontext.BeginRequest += OpenSession;3 J4 W2 O8 V x3 ^! m
.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 R' b5 e/ Z2 E4 g) l P- ^0 h4 |$ p4 Z8 o2 v. c S7 D+ F8 a8 N
//do transaction/ v7 n3 U. }4 K; Y
getCurrentSession().Submit();- f" G, C6 y' w2 o0 K: ^
getCurrentSession().RollBack();+ t7 o( f" \$ e+ x" L
...
/ G' t4 \9 |1 ?2 h, L" O% S$ WUnbind(HttpContext.Current, SessionFactory)
" }5 |+ i9 G" X: y! j; U3 N. Q9 C* W+ c; X7 W, M/ v+ J, L' l
context.EndRequest += CloseSession;% \. r1 ~/ r* w7 h2 k
( f+ u9 h' q# D' k4 ?" o5 B
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|