 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. Y8 i0 l' K! J& p基本参数:
/ R H' `3 [8 G4 @3 cjta | thread | managed | custom.Class+ Z3 I: W" [8 ?- ?2 p6 ^" H% b
主要是配合# S# t% i; o$ V- Q$ Q
SessionFactory.getCurrentSession()
) d2 w. D5 `/ L, {0 r来用的.. V+ g# x8 `; j& u5 O; Y, M
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 Q- K7 F! @3 e
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: x6 Z0 ]5 ]( bcontext.BeginRequest += OpenSession;+ _7 V5 e+ M6 g K; Y
.Bind(HttpContext.Current, SessionFactory.OpenSession())" w) [3 S9 E" X# w) v
$ K; t* D7 X4 R9 d: B" p+ E7 I//do transaction
& f# y) Z, p9 {- p& Z+ x+ P/ p4 tgetCurrentSession().Submit();" c* L9 o( B/ p e
getCurrentSession().RollBack();1 f8 q8 `- c- d
...
4 \4 L& D* M8 Y5 x6 u4 {Unbind(HttpContext.Current, SessionFactory)6 w6 I! J% l3 ~, j9 B
; l4 j7 m. `2 a
context.EndRequest += CloseSession;1 u3 ~/ F1 [0 E7 e6 i
- ~8 Z5 p8 V- K- X8 o4 L 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|