 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what& _: E2 g( f0 I; Y, Q. L% U
基本参数:
7 e' i/ V! h1 H5 q' ujta | thread | managed | custom.Class
5 \0 |/ Y$ N* K, B主要是配合! f: L5 w# A! m. A$ J5 }8 B O
SessionFactory.getCurrentSession()- z& r' V0 I* r
来用的.
& G: l1 L& x& b$ A5 r( zThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 v! P; M6 a0 I4 m: @4 a9 O& M' PWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, \6 T* m" L, Y: b( \3 E
context.BeginRequest += OpenSession;
/ {9 O, }* y; J.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 Q) p: S+ k6 h* S: C- v4 o0 w: T' u/ N3 f
//do transaction
8 W" d" F: {+ `, f+ U0 Z' K" S4 R) IgetCurrentSession().Submit();
# [' u( s6 D7 [getCurrentSession().RollBack();
6 w1 Z6 q/ O+ n% m...
& e0 w# }1 R* r1 e' wUnbind(HttpContext.Current, SessionFactory): m$ z# A' S+ g- G
' P6 i: U# t' w$ T7 Ucontext.EndRequest += CloseSession;+ P* l% G1 p0 Q3 w
; U1 C6 s4 F* E5 |1 H+ M+ W 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|