 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ T, F) t: \- p! K0 V4 w, c
基本参数:
. G. T$ p/ Q( b2 g h) }8 C! D) }jta | thread | managed | custom.Class& n( s" G, q" |2 M
主要是配合
3 j: }! @4 x' z9 WSessionFactory.getCurrentSession()
& W( j: S7 \1 B. M& y f# Y4 e3 Z3 ~来用的.# {* p) c; k' Z2 C) i" _
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 f) D0 I; U+ t K0 v# r8 ^
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& c2 y- H M f8 n$ e8 U' d+ W& Ocontext.BeginRequest += OpenSession;. h3 f) \% ^" }8 r7 d8 `
.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ z- M$ x4 s# l( S0 `1 F, ]+ @ {7 r4 z
//do transaction
2 `/ h# i! m9 Z0 l( ~' |+ \getCurrentSession().Submit();- l" l. L+ ]- A7 Y2 h' d! B( D
getCurrentSession().RollBack();
# d; m" g* i4 C: C( z...' W+ w F+ I8 D' X( I6 o0 e" [" @5 T
Unbind(HttpContext.Current, SessionFactory)
8 K6 [, P4 Q, s) s6 [ l" c! e. b+ R+ a/ _4 {( u4 {5 V
context.EndRequest += CloseSession;- r1 i0 L; d) }& c
0 S5 N& }- a ]* Q 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|