 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- C N0 f" u7 M* ~基本参数:! M- i% D. D$ Q4 K( A, O3 P
jta | thread | managed | custom.Class# W1 E( A2 _7 G# T9 L
主要是配合7 {$ \! w i5 Y0 L* o5 ~+ @
SessionFactory.getCurrentSession()5 ^: v- ]9 _: o5 l- W0 Q. H
来用的.: \7 M1 `; b) Q# a* w/ O3 l
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 ~6 H Y/ Q! }3 C+ B& I9 h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& n" b `; {' ~4 B4 _2 `context.BeginRequest += OpenSession;! {2 }: O( V$ X# `1 m" O8 s2 f
.Bind(HttpContext.Current, SessionFactory.OpenSession())2 X% p3 \& O5 F8 u# N
V- l' i' M& H% D& g8 ^9 o
//do transaction9 ?; N) ]$ Z- O* ~% d
getCurrentSession().Submit();3 A. a+ S: \( G8 x- o) `9 Q) E
getCurrentSession().RollBack();% A; {! d/ w5 E( u. G3 ]5 ?
...' Z- k. v, X- G# Z0 w2 m4 y
Unbind(HttpContext.Current, SessionFactory)( F8 e3 s8 ^8 d: `& p1 B
9 b8 v' K; k# |* J" A. @) E: E M
context.EndRequest += CloseSession;8 I+ o+ ?2 [$ s' ^% ]
2 ?4 V2 o2 h) Z4 @! d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|