 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
b" J O1 E1 Y7 A8 {基本参数:
! E; t* d. ]( Y6 _jta | thread | managed | custom.Class
: y4 H8 ]" i/ G. _- X. s0 M主要是配合
# `8 U# @: X/ }2 f s8 xSessionFactory.getCurrentSession()8 |5 {5 v: ~, Y4 m- p: L: x
来用的.7 n9 n( z, H6 g7 I" [
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" V" h0 I7 _$ `- h$ J/ XWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# {# F7 C3 {. b. N0 ^context.BeginRequest += OpenSession;
% R% o0 B, x3 g.Bind(HttpContext.Current, SessionFactory.OpenSession())
z) O! {/ B% o. ^6 x
* A, ]7 E; e$ {6 u {1 t//do transaction
8 E0 ?7 c2 E% ?2 e$ fgetCurrentSession().Submit();! a+ k, `5 E9 A& M7 |* B5 S
getCurrentSession().RollBack();
$ U4 z; U3 i9 W1 F0 M7 D...: ?- w0 n( p9 |+ h
Unbind(HttpContext.Current, SessionFactory)
# B( F% A' x/ F/ E$ {
$ Y* t, v" l* u1 e- D% W; Pcontext.EndRequest += CloseSession;3 O9 I. M2 h6 |2 L5 M
: s2 C: z" ?! ]6 z4 i3 z* b 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|