 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; @" u7 h, \1 A+ n2 a基本参数:, Z6 U {+ {" k) `; v! e
jta | thread | managed | custom.Class; V8 j' J% F, }( ?9 j$ T; E
主要是配合; @1 o% Q6 U# N. Q- m2 N# E# J% c
SessionFactory.getCurrentSession()
2 {( P- r( t! [" R2 l& u来用的.
* r4 J$ z& b$ O8 L0 JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 g8 x2 g9 @* S" `* VWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 C% G2 G. l. `4 Ccontext.BeginRequest += OpenSession;/ O3 r4 j6 \; T1 y+ b, {% I
.Bind(HttpContext.Current, SessionFactory.OpenSession()). E% K) a4 ?8 x
# k1 @# f+ N- [3 v5 M0 S+ x, b, o
//do transaction
5 s, N& d7 t9 S @% [getCurrentSession().Submit();# D5 }# I/ ~/ ^' g
getCurrentSession().RollBack();2 g- r7 x7 L) d2 C( V4 q" S: ]
...
% a9 k- D1 }! u5 q. z" iUnbind(HttpContext.Current, SessionFactory)
/ b( D6 a& H- L, e: w9 o$ n( @7 z# e3 _
context.EndRequest += CloseSession;
. h5 _2 N- H! B3 ^# U x# T8 u' I5 j) E7 |5 r7 z: v
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|