 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what" j1 F1 O% t" O S& m- W- Z
基本参数:8 q8 \* h6 s( n
jta | thread | managed | custom.Class4 d& K+ G2 R! j
主要是配合5 h4 n6 k+ x( \+ a
SessionFactory.getCurrentSession()
( u: G8 y9 W" v; l7 m来用的.% e4 L$ t+ _3 G/ n" t5 ^5 s
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," x5 P/ v; ]' T4 s9 b5 L6 }
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 @3 ^, O5 Y7 l) k7 c* C9 z1 ~context.BeginRequest += OpenSession;
: T- Z; U% g; k$ T/ v# t.Bind(HttpContext.Current, SessionFactory.OpenSession())
/ w* T$ j" t2 k+ o: z5 T# P; G$ B% ~9 X C8 ~0 {. X. D$ W
//do transaction' M0 W( q4 W: {5 d; M+ f
getCurrentSession().Submit();9 W6 l$ b; N( M' ^, D- l
getCurrentSession().RollBack();* V1 ~: `& K& b; H- ~! d( g
...
( k% b& E0 s* N" g/ \+ y( ZUnbind(HttpContext.Current, SessionFactory)
3 P# t1 h6 x1 j* `3 m- Y: I6 m6 v L4 ^7 B! F! t! g( K
context.EndRequest += CloseSession;/ b* J7 w6 o$ f( P& E
8 ^+ c6 B- R% A! A' A) H4 P
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|