 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
% k6 w7 ]3 O ^# d& X基本参数:' R+ z9 C' u3 u# o* e' d$ @
jta | thread | managed | custom.Class" P' d. P% R, ?7 l# [9 |
主要是配合
8 j- d& D2 z* D- x; X5 _SessionFactory.getCurrentSession()
2 K/ _7 h \% @4 H# f3 F4 \来用的.
. h: J# P" H+ l, v; BThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
! B$ p& m7 h+ I- b. a5 x; zWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 Y- w9 o' O) s# V
context.BeginRequest += OpenSession;
, `1 \$ O7 O0 K, c3 h, f- r.Bind(HttpContext.Current, SessionFactory.OpenSession())( C( }4 M' H$ |% j5 b! T, [
& l1 U6 B& I7 `
//do transaction
. g) y1 j0 t* q% bgetCurrentSession().Submit();
, ^, X( O- @' { m3 MgetCurrentSession().RollBack();
/ k" j) u8 C+ o/ i% h...2 o6 Z$ U" D7 u
Unbind(HttpContext.Current, SessionFactory)& g" h. j! b+ C2 Y. v
/ Z8 P" }4 f, o7 @context.EndRequest += CloseSession;
- n6 t, [: s" f$ K6 P
! l0 e0 G: w! f6 _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|