 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! `, v V0 q) r+ _" D基本参数:
% p7 C! g( m( y1 }- e8 j" w3 Njta | thread | managed | custom.Class
9 p3 ^+ k" p/ T9 l& P主要是配合
. s# H# U( Y) |% }0 l/ T8 U, B) G% }SessionFactory.getCurrentSession()* R% k5 f- }" e5 c) \7 B( x
来用的.
l* _# N- \. _7 H! zThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; u# j& ?. m/ Z! y0 |1 O0 f( P
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 S3 o( I) P0 ^$ e3 N, @context.BeginRequest += OpenSession;9 d: [ _' {; D ^
.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 y% I2 H5 v1 e# |0 `: {7 g8 t0 z% L) H4 V' W2 O
//do transaction
. C4 F: o8 ?% kgetCurrentSession().Submit();
2 i* H9 T H# S3 OgetCurrentSession().RollBack();
- @. x7 c4 i7 J/ @0 b9 w, n, c...! `' W- I9 n+ h9 O- {. V
Unbind(HttpContext.Current, SessionFactory)4 b0 R, |; Q1 `: B. t8 P
2 k+ s6 n& f2 g9 T Z
context.EndRequest += CloseSession;" m* N( `4 _ y8 |
7 d \* o1 ^( O+ R$ Z: { 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|