 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ K* @- R! j" O% @
基本参数:
. j% _8 s2 d2 |% C# A, A/ [& njta | thread | managed | custom.Class
$ s5 M7 v+ D( G" n' p u主要是配合
% k3 q. p' h4 q1 z; p. aSessionFactory.getCurrentSession()
. l$ j6 B& a# d8 ]; t% _来用的.
; u+ a& o0 ~/ z3 `4 o5 Y( L9 MThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," m, \! l1 k3 T# `
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( q: ]* K% b9 [: r2 X5 R6 I
context.BeginRequest += OpenSession;& y4 ?; y: O3 \8 S7 j
.Bind(HttpContext.Current, SessionFactory.OpenSession())
% X3 |3 ~' ?1 B* E9 N- I4 \, @7 M( P4 m; Y, T" b7 j& k
//do transaction
( U) C% t7 ~+ m9 A; ^5 jgetCurrentSession().Submit();
' W* N4 n) O: G3 U v, ugetCurrentSession().RollBack();4 V$ l5 B8 w j0 k7 m! I4 ^% V
.... A2 y6 Q% L. Y/ r( u
Unbind(HttpContext.Current, SessionFactory)
" F( `: E( u- p7 E7 Z9 D4 t+ Z% B+ b
context.EndRequest += CloseSession;
& S1 G$ L& b6 d V5 S7 c: G: _3 v( @6 W! b
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|