 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
' w8 j! b. x1 z6 w" h基本参数:
( B$ s, ]" n7 Xjta | thread | managed | custom.Class
: H, o( p+ R+ q) ~0 |$ Q B1 x9 j主要是配合1 {; T0 F. }$ G; f, {
SessionFactory.getCurrentSession()
, r: r0 }6 ?, \; @3 I9 P来用的.6 F! I( \% {+ j1 Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
( o2 ?8 w! }6 e6 F% U' eWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
" E' k+ v2 J- v* z1 e# `) Icontext.BeginRequest += OpenSession;; E d6 \! a: A4 _* y. n
.Bind(HttpContext.Current, SessionFactory.OpenSession())% O0 A* U% Z8 L9 ]9 q. l
/ d" L+ R2 g7 E$ @5 |$ A//do transaction0 v! ]- T t3 C3 K' V! E/ ~# J B
getCurrentSession().Submit();! d0 ]: V* I* T1 J# Q2 v, k9 q
getCurrentSession().RollBack();% r, j1 B- ]5 P$ N
...
% ]1 _! z+ C. B& K& k+ SUnbind(HttpContext.Current, SessionFactory)# s8 G& F& ]/ M; Y/ s3 {& a, N/ J
/ g8 |9 V4 b5 F% X' K
context.EndRequest += CloseSession;& g3 m3 r: H5 o
% w. g; d/ n: Z
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|