 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# Z; s. k+ h( _/ h. V基本参数:: T$ Z3 C/ w5 {- }5 s, P" H
jta | thread | managed | custom.Class
4 J/ i4 P( H3 j0 {) D- e主要是配合
6 F' n2 c% d0 Y, e* |+ hSessionFactory.getCurrentSession(), x, _( a4 s) I0 p
来用的.
# q' @9 m* x2 C' PThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ r9 h: H( J: L) g8 @0 i
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' r5 A4 k0 T% ^! Q; t
context.BeginRequest += OpenSession;- K+ r* O6 {2 m" `: U
.Bind(HttpContext.Current, SessionFactory.OpenSession())/ U5 @7 C8 H4 J) @) W4 _4 o0 s
: _* K* ]) {- x8 ^1 | h8 D H" M//do transaction
g% ?' H. C R& j4 |; l3 V" FgetCurrentSession().Submit();
4 @0 i" _, R, rgetCurrentSession().RollBack();
) n3 m- }0 q @5 i( r0 h...
8 @5 a. ~. `0 a( `Unbind(HttpContext.Current, SessionFactory)
5 r g/ e. |3 p5 l% n& Y$ a" C% E5 U& ?
context.EndRequest += CloseSession;; {5 w& `1 r. f8 s
' E* P% ?+ h$ p# K+ |
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|