 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
% G; b$ ~' {! U1 n+ m Q基本参数:
; R# E [5 v- y/ f, o8 T# _ Njta | thread | managed | custom.Class
! m Z/ X9 r+ F, Y5 Y3 F主要是配合1 c5 ?3 |+ F# n" u6 @, ]
SessionFactory.getCurrentSession()1 t, `/ M2 l. u/ V( t
来用的.
- u2 t; N6 _6 P" C- oThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确, p4 u. { v$ M1 _+ g' J/ c
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
* `/ m# m! G* ~' G! _( X! dcontext.BeginRequest += OpenSession;- f" y6 F9 E& H+ n
.Bind(HttpContext.Current, SessionFactory.OpenSession())
, p. P6 S9 }, ^! j4 S" e% y
% v9 C* s, }: V# C//do transaction7 t/ o6 P- u7 G. v" z& {& q! n
getCurrentSession().Submit();
- C8 \+ U/ G* }+ x1 X6 U3 @getCurrentSession().RollBack();
- O x% m0 q5 g# A8 [4 `$ n...3 \# c6 D7 h% g/ l) k/ M' o
Unbind(HttpContext.Current, SessionFactory)
% G1 D q" x( A! r6 c' W; E3 E$ n) S# T
context.EndRequest += CloseSession;
; e8 `- L' I6 B) Y' m/ H( V+ K2 l E; y0 y. t
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|