 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: {: }9 x0 U4 S5 L# l- b
基本参数:
. p: [) R) i5 g) S. Sjta | thread | managed | custom.Class5 |" h- t0 B- s: _! |- z
主要是配合! w$ k5 H% r$ q1 W9 ~( {" e4 O
SessionFactory.getCurrentSession()2 _1 B5 g+ H* K) W) q: r' U
来用的.
, k! K2 G/ y0 [$ JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,& J7 h3 W) h5 _* W2 H
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:+ ]0 Z! p6 X4 ]* P$ v+ ^( J
context.BeginRequest += OpenSession;( u& F# l: x3 f6 W; ^
.Bind(HttpContext.Current, SessionFactory.OpenSession())- |; Q4 ] N9 J" {4 j: Y. m
) A( C! \" L' @ ?- B9 j//do transaction
7 u( V: ]7 |) ]' A2 K( W$ OgetCurrentSession().Submit();
! q& R, J6 L/ ?# \7 G1 Y( c; pgetCurrentSession().RollBack();* W) w7 K3 z& \4 A* D' p! d; s
...- C3 L7 q: e, A/ v3 H- o2 p. Q
Unbind(HttpContext.Current, SessionFactory)+ m4 p8 E1 e1 A- _# J* U5 @ p
' O0 N+ g: U+ b2 h. X4 s4 P7 O
context.EndRequest += CloseSession;* j! _ m9 K; y
3 |; G9 ?; G& K- W" n 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|