 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
$ Y. } J) O" k& x) a1 m$ M3 C9 l基本参数:1 `/ Q e1 ?# }8 e
jta | thread | managed | custom.Class
( i3 G1 v/ d3 z: p: E6 L( | w+ s主要是配合- w i+ y1 g( K6 y# E
SessionFactory.getCurrentSession()
$ Z- L4 _+ m- n( s- e来用的.
# \9 }/ c3 `4 o. r" aThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,8 H2 `8 m3 y+ I* a: G z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( n, t/ Y0 Q% m0 D# u `
context.BeginRequest += OpenSession;
" m5 x! {$ ]( k/ }.Bind(HttpContext.Current, SessionFactory.OpenSession())8 B. Q+ x; F& O. T" ~9 @3 L8 n5 T, Z
0 L& D( a! t l" `) f; A4 M% X9 G
//do transaction
" s3 ?, y+ c5 K/ ZgetCurrentSession().Submit();
: I# ?/ {- U# k/ kgetCurrentSession().RollBack();
$ [2 H$ p+ |$ F3 K...
3 C; s" E2 K: j6 u! n DUnbind(HttpContext.Current, SessionFactory)" O8 u5 }* v" i6 n' \ V# I3 k5 ?
8 H" a( U! P9 |( r8 x' y) s! z
context.EndRequest += CloseSession;& x( N- e; |, X4 d- ~+ w
; ~ q- H; d( U/ T! H( J/ P1 p
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|