 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what7 t6 _7 v& O+ ]8 g& F! k
基本参数:
: E h/ g' @; O5 pjta | thread | managed | custom.Class
' ^" l3 U( O+ T主要是配合
9 W; C6 V7 F$ ^+ j$ ]# d- x0 gSessionFactory.getCurrentSession()+ X6 ?' z+ t# @' ~) j, s
来用的.1 l. R: R# ~1 c! @
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
' D7 }: G( ]: Q$ M) qWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
, `+ S/ q4 P8 }/ f& t2 D: g8 icontext.BeginRequest += OpenSession;
* T. I7 I% Q) W0 y.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 M; Q' K# v* ^- p: A# W' B' ~' \% S, U" f/ T3 A$ X1 E& U( e8 R& z
//do transaction" d: d: A: G1 \
getCurrentSession().Submit();& W* ~0 {* L+ Q# `$ U2 P! j; p
getCurrentSession().RollBack();
, X" [! N8 T4 ~0 q...3 O# m: z3 H, z7 s
Unbind(HttpContext.Current, SessionFactory)$ V' r' W# v$ X! s3 S! K) {; }6 N
3 t5 _) h( L0 k
context.EndRequest += CloseSession;# x8 l/ x1 I, \+ \% f; N
9 R2 m" F/ X! F T$ p' s7 _
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|