 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ ^" c; j" p7 d基本参数:
9 v5 y% b- E! @7 t3 _jta | thread | managed | custom.Class
$ ^2 x7 m2 }4 P主要是配合) F/ t7 p2 I+ u( [8 d! U, v
SessionFactory.getCurrentSession()% r' F: R" N6 D+ H
来用的.* c0 }( j$ A1 o+ j7 Z9 I
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,5 F. x* V5 w9 u+ Q9 R- H; p
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
0 S \& W3 g, P, p4 zcontext.BeginRequest += OpenSession;: c6 w( E) v0 \3 T" x+ F. ]! |2 s) o
.Bind(HttpContext.Current, SessionFactory.OpenSession())+ u E* ]- n/ U) z2 [6 B- R
4 {* _. W) A. a$ r% c
//do transaction
+ v e% v& T9 c' zgetCurrentSession().Submit();
0 k$ ^5 Q4 f9 V: wgetCurrentSession().RollBack();
" B' p/ r2 R- O...
2 [: Q2 ^) l7 {& OUnbind(HttpContext.Current, SessionFactory)
8 \) M! @9 G" ]" k% O: c$ O
0 w8 F1 C7 `& |2 K9 ^5 Q! [context.EndRequest += CloseSession;! ]! W2 n. m" b( e
* {/ T- c% J1 x4 k( j0 A 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|