 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 j0 j' {. V7 r# m2 U$ [6 c4 J
基本参数:
5 h7 | [( j& ~4 F5 f# ajta | thread | managed | custom.Class
4 W) B% z' ~* `* ?) b. ?" ?! f主要是配合
3 u+ i3 _' [6 b: y6 H# iSessionFactory.getCurrentSession()( ^' s9 v' o: x4 {& l6 n9 m
来用的.( R; ^. _! f* q: X. t% \
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; |6 U6 F: h# I3 A# }
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% Q: k' N, H" _ P. U
context.BeginRequest += OpenSession;9 \6 F) u# e: K# _% P0 j O
.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 |/ x! u3 F$ ^* s- }& P5 I7 u
; v$ H7 m& Y" C4 ]//do transaction
, v: [$ D/ l9 QgetCurrentSession().Submit();
" x0 n8 s: A% o2 G/ R9 L7 n2 A1 agetCurrentSession().RollBack();' v3 |/ i( T" D
...
+ e" V! k: Z4 s5 n% F, LUnbind(HttpContext.Current, SessionFactory)8 f- G: D: U- W% j8 K
3 T" y. |) O6 H% P+ M2 R
context.EndRequest += CloseSession;
$ n0 J% X' o3 p4 B' I
8 k k4 ]/ q( z9 _" g: u( A p. ^ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|