 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; K& J; u [9 y2 n" G+ m7 C( |
基本参数:
/ ^5 e+ A5 G A1 Fjta | thread | managed | custom.Class
9 R- M9 E$ {$ @" w) J6 _主要是配合
" r$ a3 K& z( V$ ?4 Q8 kSessionFactory.getCurrentSession()3 _+ ~" Q. } g, K& C
来用的.
4 z" s/ W' y: D# D+ R0 e1 g2 |Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! ~( f1 Z2 X. `/ T6 R7 J
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* L5 {+ w* a+ e" W1 ]
context.BeginRequest += OpenSession;
; g6 h2 R' }# j$ A$ b.Bind(HttpContext.Current, SessionFactory.OpenSession())
@8 u9 X( f3 @! p4 K, i
. P( R. a7 A7 R0 i$ h//do transaction
# j9 q, _! R1 |% q- Y0 ]$ f" [getCurrentSession().Submit();
5 \- J2 A# ~; agetCurrentSession().RollBack();
7 v3 N$ O' V. t/ ^' g. p2 c...( H9 \! W/ `4 f0 r, ~! B
Unbind(HttpContext.Current, SessionFactory)- d/ N7 c; q. m) }
/ @( x' `1 i. \! f' ?4 f
context.EndRequest += CloseSession;
- \- f5 K t) U. U2 o) y
0 `; m7 o: N+ r! z4 u 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|