 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
9 ~; i/ y k) I) y+ ~$ x基本参数:
& ?0 j! q+ |7 c6 P2 G5 fjta | thread | managed | custom.Class# ?2 p( S0 E& g! _
主要是配合8 ]# d5 g9 f( `; |$ Q6 k! l. b- ~# _) o; J
SessionFactory.getCurrentSession()% ]& [- J7 w5 E( ~: I! p B
来用的.1 m+ O/ u5 T# t# A& s* U7 ]7 M
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" e' \" \. m; C+ |+ DWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 r, ]7 [; d% V+ b6 q5 v- c
context.BeginRequest += OpenSession;- P/ @. H$ N6 b! X7 W3 M: E6 H# y
.Bind(HttpContext.Current, SessionFactory.OpenSession())9 y( r+ v9 U2 u. N1 S7 ^
" Y, _2 o- m# c$ R4 A
//do transaction7 E) N& b0 g9 w* `+ _* r
getCurrentSession().Submit(); @2 [# I( L6 |; u
getCurrentSession().RollBack();$ n6 H9 e3 N! x5 } x+ E) w
...
5 i" l+ r- D+ BUnbind(HttpContext.Current, SessionFactory)
7 q, Y7 f/ Z9 V: x
- h) K) A6 H9 e7 O) kcontext.EndRequest += CloseSession;
( ^$ m# z" F% H4 `/ U$ L6 s( C P& s# O+ ]# i
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|