 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. C, s8 ~3 {9 y+ H; z* x
基本参数:
7 x z% q% d# E5 Ojta | thread | managed | custom.Class
. m7 b$ \/ b& i/ ~9 ~主要是配合1 W" m. c3 w; b) p3 V! b/ h
SessionFactory.getCurrentSession()& s7 Z4 P& X* o/ |; ~7 B* ]
来用的.' x0 a7 M$ f; ]; c1 F- \5 a4 v/ W+ E
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,4 y4 `- O1 f8 b/ ]7 A
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 v9 ?4 s. e6 O) f; ]8 D
context.BeginRequest += OpenSession;9 z* t3 @8 G( H. Y0 P5 ?2 p
.Bind(HttpContext.Current, SessionFactory.OpenSession())/ Y" Q: ]; @4 G' S
0 }( ]$ ^8 ]4 p6 c+ ]8 n//do transaction' @1 V# F/ A4 p. o
getCurrentSession().Submit();
`& X8 ^; S. y6 l; E* e5 CgetCurrentSession().RollBack();
9 K" s/ V3 f/ e7 P" b. r3 E! x...
( m2 K- t6 @" i4 [6 B9 qUnbind(HttpContext.Current, SessionFactory)
* f# a( J3 |$ s/ G' ~
/ @8 c/ e7 o9 Z Y# `/ pcontext.EndRequest += CloseSession;, m( O S& h/ ] y3 x) t C
/ r( T+ O3 B( Z `/ s% c3 l D 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|