 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
$ k2 m7 V4 _) u* }- z基本参数:
) z2 V- J# n7 }' Z/ N; Cjta | thread | managed | custom.Class( P8 I! W$ n' ]. {- e. ^
主要是配合" V$ p% S8 N% U+ ?9 a% [6 i
SessionFactory.getCurrentSession()
9 x I C$ A0 `& k6 K$ P来用的.
, t- e- ~6 U8 y1 ?Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
% Q( X; q1 d+ e) |) uWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; d7 Q) d4 f# P, C) D! Bcontext.BeginRequest += OpenSession;
0 o7 d4 `& V0 c.Bind(HttpContext.Current, SessionFactory.OpenSession())
7 {; A9 G7 m8 w$ G8 p4 _, q9 M. j1 E8 j6 i9 N1 O6 K. U* m
//do transaction
7 A# s! `9 @9 V; a! [3 K% R2 xgetCurrentSession().Submit();
' K* w) |* k9 z D4 }getCurrentSession().RollBack();6 J- i3 x8 E; i4 n7 \3 O
...# ^* ^8 p1 U, q
Unbind(HttpContext.Current, SessionFactory)
# d6 f6 z- [7 ^% i% U5 j% [0 g3 X N' c" ~& s; V3 ]# z/ V
context.EndRequest += CloseSession;
* p8 i4 L9 v" W, P8 J2 w! n% l9 F0 [1 y2 P) S# U; ^! W# @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|