 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
5 \7 L2 G$ |" r4 q3 N; ~, J基本参数:* @. b; c; P% B1 H' y/ A
jta | thread | managed | custom.Class
/ d- w6 X6 L( v" p3 C/ {主要是配合! a' Z! H1 b! y8 V+ B
SessionFactory.getCurrentSession(); s& S B2 @! R& G8 ?4 U
来用的. w0 x9 h4 S& E8 w3 a8 ]5 I5 b
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" Q( ~, k. i* wWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:4 A" H- q7 c9 i8 C$ y4 D
context.BeginRequest += OpenSession;
. }8 o" A* x& O8 K: o3 }. ^.Bind(HttpContext.Current, SessionFactory.OpenSession())" ]6 R, p, ^2 [' F; g0 d. _
# a* d' A7 N, a9 X//do transaction6 ~! u- @8 k. |7 X/ `6 _
getCurrentSession().Submit();/ f( G3 F# s# @1 a
getCurrentSession().RollBack();( |" i! V& e# s
...: t5 @; i# V2 r/ x0 t/ [& N, H
Unbind(HttpContext.Current, SessionFactory)
2 q% P4 c( W8 G) x. }, O1 C- Y4 q9 C, l7 t3 P
context.EndRequest += CloseSession;) D# U" i/ B2 a5 W0 C7 q
+ w2 v- y9 a- M2 O w4 I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|