 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 q& ?) s" a5 S" X5 h
基本参数:
9 R8 Y8 |; W( v7 Bjta | thread | managed | custom.Class: v0 B/ \- L F: n7 a
主要是配合
H) J0 S: R0 \: L7 M3 wSessionFactory.getCurrentSession()
; I5 S! R% }/ }: E6 S" u# j& N来用的.! v' W+ b! D9 c* V0 @; {
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) o) C- S" @: m" w5 J8 JWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
% }, ~# {/ o3 J1 o8 O1 X. _context.BeginRequest += OpenSession; I6 o) V3 D. b7 q* K. S
.Bind(HttpContext.Current, SessionFactory.OpenSession())& T$ Z. F/ R5 M9 M
8 w/ U/ i" X2 M6 b" l
//do transaction
, o/ \" v# B1 r G& ogetCurrentSession().Submit();
2 X7 i% j4 k/ E8 P8 ZgetCurrentSession().RollBack();
1 ~% Q+ X \/ u! G/ @7 f8 o4 b- S...
. E3 a' A+ A. sUnbind(HttpContext.Current, SessionFactory)
2 Y8 k. i- Z |( {4 I8 s. b/ h, D" L% g2 T
context.EndRequest += CloseSession;' A: i, f! I' n" I+ E* @
" N% N8 v5 |7 r4 G
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|