 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# u% v, h1 j& E% z基本参数:
) ~3 i! |, Z5 b& G/ b, ]jta | thread | managed | custom.Class
8 b( M! K1 g6 c) {! L主要是配合1 j2 s* a4 Q5 ]: s
SessionFactory.getCurrentSession()
9 c: [3 l2 u; k1 }/ w+ g1 D4 l来用的.
7 v! P! v$ G$ v# YThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- A; V$ d7 j: }7 ~$ QWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ g0 M* J! Q2 z9 S Gcontext.BeginRequest += OpenSession;
. ^7 J7 s7 x# [ Q; [4 ]5 {" S.Bind(HttpContext.Current, SessionFactory.OpenSession())
; r5 [- z9 [; T# w' [0 x8 `. `& h& o5 \, M# f) s, [
//do transaction
, ?6 H0 C+ Q# h3 LgetCurrentSession().Submit();' Y8 D2 k, ~) [+ w' ]6 g/ G6 l: Q+ R+ |
getCurrentSession().RollBack();
# F9 K4 O# T! \3 J...
1 |: x/ u$ H, p! @) t% tUnbind(HttpContext.Current, SessionFactory). ~; @( ~3 a) Q* S& u
8 c3 j7 a; E' B2 @8 w
context.EndRequest += CloseSession;6 C) ?8 Z! O) ~+ J7 P$ _; [
- q B( H5 }- b' Z5 I& s
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|