 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what q3 R+ V! R) x5 e/ [
基本参数:* Z5 p, ~( P% d' R5 \
jta | thread | managed | custom.Class/ `) ]: Y. e/ H' J" Z% b: M( j
主要是配合
! X# k; z! L8 N& S6 USessionFactory.getCurrentSession()) M0 ^4 o# H6 A4 U# f
来用的.
# h. ?! z, Z" A' E/ kThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ X+ X$ {$ z% N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, R: _7 ^8 Z$ D9 k2 K
context.BeginRequest += OpenSession;
: ]9 F% S$ \( K( w+ @& E2 Y4 N8 H* ?2 ~.Bind(HttpContext.Current, SessionFactory.OpenSession())
' B* ~6 \+ e1 v% v6 L
* v/ M N) `4 y( O) t//do transaction% _+ ]& z9 Y3 ?4 W7 C: \
getCurrentSession().Submit();
0 A; c, e$ m+ o/ qgetCurrentSession().RollBack();& X ]9 Y( F3 x1 t- p: C9 F4 j
...' x8 a* W, p6 Z; m& w" a9 N
Unbind(HttpContext.Current, SessionFactory)) D7 c' U) s% e5 q* J
3 d2 h" ^* Q# H- m8 g7 s
context.EndRequest += CloseSession;
5 C# V3 G7 ^% Y. f8 _, k
" T8 [5 V8 d- ~8 z% j$ | 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|