 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 D% h) `1 c$ k7 h" o
基本参数:! D& ~) }3 v/ i q
jta | thread | managed | custom.Class7 S* J! v" O4 ?$ t' p6 A
主要是配合( Z8 O9 l! t1 ~( q
SessionFactory.getCurrentSession()
: ]- ?- g/ F. @7 c# T, U来用的.2 M( J3 e1 {4 x$ P* @
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ T. ]4 R5 l# V6 Z- z, M7 @
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 D' A. o$ I* {0 {. f# Lcontext.BeginRequest += OpenSession;
# A4 n' }, h3 L6 t.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ x; m* P# Z* d% D/ y. f
" u1 I' @6 ?: i- Z//do transaction
4 f6 z K/ O. mgetCurrentSession().Submit();
" k' R* w* V2 n3 K" QgetCurrentSession().RollBack();
% J1 ^- m" _+ m, H+ J..., Z4 H0 T7 q7 F7 R/ B+ v4 k
Unbind(HttpContext.Current, SessionFactory)
! X0 L6 R& v6 w
: b2 m$ S0 e% ]9 ? x4 q+ u& {context.EndRequest += CloseSession;
. o9 v0 p2 Q- Z+ `: k1 ^3 c. T9 ~# F* G5 @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|