 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
9 _. R& U: Q% E6 n基本参数:3 m; o5 J9 e3 ^( l7 o0 R+ F0 K' u
jta | thread | managed | custom.Class
5 n& z; o0 @+ P# `9 w4 [主要是配合
- X- ^& p0 }* v. C0 H% f# o& A! }SessionFactory.getCurrentSession()
. a4 m: `% l+ a. A$ d来用的.
, I/ S2 X, T5 `& ~7 ?8 h2 d, |# tThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. z& h1 `+ ^4 p8 L) _% C9 j0 l
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:" K$ t0 i ]2 b( g& p9 h5 n
context.BeginRequest += OpenSession;
( j% ~. r, ^3 e* a( d.Bind(HttpContext.Current, SessionFactory.OpenSession())- [1 j+ \4 R% f2 e* v! Z
& p3 q+ u- Z' v! j
//do transaction
' \, p9 }9 @' W0 l xgetCurrentSession().Submit();2 y2 @6 ~; Y2 m2 Z) r& `' I5 I6 }3 y
getCurrentSession().RollBack();
9 \ e: E/ \1 r! Y2 F...2 c! w' I: h9 ~/ i% @* i9 g1 R) }
Unbind(HttpContext.Current, SessionFactory)
# Z) [ v6 q0 r {8 f& p4 s
3 J3 c7 }' w1 C7 w& Q- gcontext.EndRequest += CloseSession;
6 a- v+ f. p% A8 F3 i% I6 x# [. M" d9 e( W
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|