 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
3 V. w- @* a! X% ]基本参数:
) K6 p$ E" x6 U |/ ojta | thread | managed | custom.Class
: l5 Q, c3 C4 K3 a8 ?7 D: ~% Q* m主要是配合5 G# E& r9 Z* N; _/ _/ k3 ]( ^9 x( i
SessionFactory.getCurrentSession()
l# `. b+ N; R- n6 [" H来用的.+ }+ ^2 P+ ]+ q7 I# f3 o; f1 ^+ N
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 o# _: A# c/ V# ?5 T
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:- a- ?) R6 L% O5 h% s
context.BeginRequest += OpenSession;
6 g: m% V2 x' n: u& S, M5 T d( _.Bind(HttpContext.Current, SessionFactory.OpenSession())
% |0 b- R" d, y( L0 z1 m/ V9 v: m8 f, I# `3 J
//do transaction
5 h8 ? v5 T' F& I9 B' L% B; bgetCurrentSession().Submit();
/ i6 ^) }, y1 Q% f3 vgetCurrentSession().RollBack();9 m7 N4 E8 V4 `
...4 T5 d0 j. W" v
Unbind(HttpContext.Current, SessionFactory)+ _; `4 S+ l9 q, O2 k
9 P6 w2 F! L7 l
context.EndRequest += CloseSession;
. Z. E; w5 d& O# `: I1 s9 p" L A+ D7 j, e, b7 w# V5 c
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|