 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' S h! J; j/ g
基本参数:
/ y8 U5 p& V6 S; [+ j: [2 m$ Ijta | thread | managed | custom.Class
/ P3 N9 P" M. m$ ?0 B主要是配合
6 s/ P5 L d! q7 P0 `SessionFactory.getCurrentSession()* ]: P8 D! X7 {& q; f
来用的.
- Z# _0 P7 g8 \3 \Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; a5 T; Y- t. M% G
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) `5 l* B# N) \
context.BeginRequest += OpenSession;. {$ x% Q- n2 w
.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 ~$ ]& Z" h' C+ q! M# Z4 H: W! l: p" B" j# n
//do transaction. u5 \+ q* c8 p- s
getCurrentSession().Submit();, n7 ^$ t k7 _2 ]0 h8 i
getCurrentSession().RollBack();. j8 k) @6 @' x+ ^: L) ?
...+ S- s; H/ V' y, O# |$ b4 }$ D
Unbind(HttpContext.Current, SessionFactory)
: w$ ~6 Y. W* e2 Y! ^, p n% G+ S
) [) n5 \, O3 L3 g$ N0 ?context.EndRequest += CloseSession;9 Z" Y# b" ^: g4 E' m6 @
$ u0 T& C; N0 O) ^. i 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|