 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
3 C3 T2 t+ z) c* V: N0 ^ X基本参数:
: j$ z$ c/ p& cjta | thread | managed | custom.Class
' N0 [ X$ M; L$ N5 A, w8 L主要是配合1 X- B: ?. b9 H9 g; s
SessionFactory.getCurrentSession()2 Y+ d! U# m/ Z' n- E4 n$ m
来用的.
# M! x7 [9 H/ @6 oThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: t* ^+ ` y; z j0 jWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:! W& {1 H) [ h/ P
context.BeginRequest += OpenSession;4 C; G8 {) l0 _5 \/ ?4 y
.Bind(HttpContext.Current, SessionFactory.OpenSession())
# l% A, I& x0 X1 F9 o* i ^1 Q% @/ a3 P( B( J$ S, D' X" x
//do transaction3 x, Z- u. s" v% f+ d, V( o2 u
getCurrentSession().Submit();3 `- q6 v: y5 M* s" H7 ]. L$ G
getCurrentSession().RollBack();
! k q+ W. w2 [9 `) Y# n...+ c/ u: E. [7 D. B, S. U, L
Unbind(HttpContext.Current, SessionFactory)
0 d5 o9 S) d% w& C" g9 J8 {2 A; C" q u! s, \" a5 T( o& K
context.EndRequest += CloseSession;2 |( U0 k. I0 C! @
0 }: f7 _; S/ X 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|