 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) p! R6 g( @' }4 ?5 [' h I
基本参数:& K& K* _' W- ]2 q2 [6 Q: A
jta | thread | managed | custom.Class
& X4 w% Z! h! B v% h: ~' m# F" v6 b主要是配合! i1 X- H: F7 c, A; V, ~
SessionFactory.getCurrentSession()6 Z4 m/ \' W. ]6 [. f/ ~
来用的.2 M% O* `; r& [) U0 }0 D
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 T! y$ }) v4 T5 `# H" f
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 `- X- L" P" H/ J4 W7 b
context.BeginRequest += OpenSession;
( M: Y& b3 G* i.Bind(HttpContext.Current, SessionFactory.OpenSession())
: @, a+ y& F+ `$ N
* a4 l0 [ G2 Y! \3 B- E//do transaction
- z4 N, s3 P, p* FgetCurrentSession().Submit();3 G, ^) [5 z# T' @4 U/ J7 V$ a
getCurrentSession().RollBack();. \- M+ N1 ]' K6 K* \
...
) Y4 Z& D! R/ RUnbind(HttpContext.Current, SessionFactory)
- }0 z/ T& s! \: l% N8 t- e8 c& U6 O p9 s
context.EndRequest += CloseSession;
9 x! o6 `: B( m" `8 a
3 F$ Y5 V# Z2 {8 [. i 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|