 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
, P; f1 O6 |2 u8 `基本参数:
6 }% Q) d5 G; n' e' Xjta | thread | managed | custom.Class7 p( p M9 _$ a+ Q
主要是配合
4 z6 }# u) K* PSessionFactory.getCurrentSession()
* f4 d% W2 p! b, ?/ e0 O来用的.
( Q( f5 v+ g* E! m, j: F6 H1 z2 v' uThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 ?% R% G u; h9 s* Z' O
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
/ z4 c& e- o7 q1 w+ X* `/ ^context.BeginRequest += OpenSession;( c" v0 ^4 D9 ~' x
.Bind(HttpContext.Current, SessionFactory.OpenSession())" _6 t7 s$ o- ^/ [' i* {
/ N9 H' {) R; o//do transaction/ _: Y& P3 W$ Z
getCurrentSession().Submit();3 a; W3 q, Y, B5 m1 S( b4 w
getCurrentSession().RollBack();, u" W, ]/ _- E4 \5 w
...: f" h. [# ~/ X: j
Unbind(HttpContext.Current, SessionFactory); y8 q) [4 p3 K
) q, J$ f+ n. r% ^
context.EndRequest += CloseSession;# u9 h! ~ @& t% W
. B2 h6 u) a( v 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|