 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 p' O- H& @" a0 k. u$ w基本参数:. X8 A1 z# j0 X) [) X6 x6 o' P
jta | thread | managed | custom.Class
9 F, J* P3 M4 @+ V8 F& j- W主要是配合6 w+ R, F6 N% L3 T
SessionFactory.getCurrentSession()
e5 J6 u0 I+ |9 v来用的.
) {9 R# f; G' ]0 W4 k& c& @9 K( }Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
4 g$ w1 R: S+ g* AWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
' g, {3 B3 G9 zcontext.BeginRequest += OpenSession;) h g' u5 s) f. `8 T7 } m8 K+ m
.Bind(HttpContext.Current, SessionFactory.OpenSession())' }. Q( u7 a% b" { n: r# `
6 b) [7 ~7 Z9 j5 z0 E g: w//do transaction
( M( y+ n. ~4 ^, u( {/ R- a/ XgetCurrentSession().Submit();
; t. K9 X X, G5 i7 Y8 u, YgetCurrentSession().RollBack();" L2 a9 ]! x' I: A/ W- M. i4 Y4 J
...! h- l# y& W. P
Unbind(HttpContext.Current, SessionFactory)
# b O- A9 m1 d( s9 K# v0 ~8 Q
; _6 X( p$ F5 k- p: S2 Vcontext.EndRequest += CloseSession;
6 \9 M \) f" v G7 I
9 s; {7 s9 ]1 O% T2 J0 i: g3 _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|