 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 c: q- r3 H0 Y% L* V2 \4 K. R% q
基本参数:
6 l, N' F3 ~, u" V, c/ Rjta | thread | managed | custom.Class
7 `. Y7 w2 a- X: y/ f9 [- W* x主要是配合
7 d V1 p% G. A+ ~6 [/ @SessionFactory.getCurrentSession()* M/ l ]3 f; Z6 k" i {, \
来用的.
6 u- y9 n7 x' |2 \8 A @& zThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
/ Q9 |; N! d. c$ i0 u! D) RWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:. g1 V8 b' W2 |4 C: W
context.BeginRequest += OpenSession;
( q, T' I# w7 \ n.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 _) s/ j. S/ i, r, @* P
, {. R4 ]* T( m0 \5 c1 X* e//do transaction
( }4 `. T! J* y1 J- {8 G' FgetCurrentSession().Submit();
3 o/ M" }4 Y. I5 n$ s/ |getCurrentSession().RollBack();
# ^4 Q/ O8 s0 v1 X! p. ~' f; B* e...9 i. W" x% Z! E+ T% }5 x
Unbind(HttpContext.Current, SessionFactory)
8 O) w1 Q, y! m# M) F" ^- S. T' N# e' C: x" r) _+ u
context.EndRequest += CloseSession;: S$ w, ~% X! g" e/ p5 m! a7 n! H
4 ~( z) E9 x! _! k) k* G
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|