 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 F/ C: \) {! n/ b基本参数:$ ?# D- R. f5 E! p( m0 N2 R5 R: Z
jta | thread | managed | custom.Class
- R: ?* K+ Z6 f6 e主要是配合
2 g/ O5 e3 `, `' F. P+ oSessionFactory.getCurrentSession()4 e5 s- r+ C& t" T- [) q) i
来用的.# Y. @2 A! s/ q2 L, U+ ~+ ~) ?
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) R6 l5 L z# Y1 N, e2 N6 @Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 [3 H! [. Y& k; [6 Z0 W0 Y. h
context.BeginRequest += OpenSession;: T& ?8 @. n+ D- Q7 i' s
.Bind(HttpContext.Current, SessionFactory.OpenSession())
* Y) \ K5 T" `- v2 Y, R! g6 z$ |( d' |1 U* ?% R- d
//do transaction
8 T+ r$ i/ q8 B4 f! ]3 XgetCurrentSession().Submit();
4 H8 s* T- t! v( X; ?$ t! CgetCurrentSession().RollBack();7 b' \0 t( \& m# j/ \
...8 j- `, Y) Q; d9 f
Unbind(HttpContext.Current, SessionFactory)
7 X: a* c2 ?& ~+ _0 w, w# j, L( F. D. ~4 y
context.EndRequest += CloseSession;
4 q" x: q& Q8 D+ l% n
' L" O8 X/ @; a$ i% q' s 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|