 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 a% O0 ?/ t& T1 S5 X) a0 N基本参数:$ Z$ p0 L/ y1 `0 {
jta | thread | managed | custom.Class
, u# R: T2 D3 i# U主要是配合) t7 z: D p) q, O$ K: g& u
SessionFactory.getCurrentSession()
5 N8 F5 H9 t" _. Z- Z来用的.
) H1 D" f* H8 A: fThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,* B% q' D2 X# N( U0 M
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:5 Y+ t% d$ I9 c% N8 ^3 C
context.BeginRequest += OpenSession;
$ y0 m! j/ H4 O% n.Bind(HttpContext.Current, SessionFactory.OpenSession())
. ^- B. x5 j" e' ?3 g: L+ o: z. `. }7 t* g4 r' A8 V9 v
//do transaction& V6 g( Y- H8 c& g/ ~
getCurrentSession().Submit();
# e+ J V* E8 p4 Q5 G3 ogetCurrentSession().RollBack();) a, i5 u7 m8 A. q
...
7 N( w. i2 l0 G0 S1 vUnbind(HttpContext.Current, SessionFactory)
7 Q5 A* \" u+ p. @( w/ ~; Q" T5 i
% H& |& J4 i5 _1 @' A' U- _context.EndRequest += CloseSession;
* U0 n4 L) s( j5 C0 ]3 {; }, M/ t! `* O$ o1 n& z
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|