 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 _0 t" e" j& @1 h6 W6 V. |9 {' k6 Y
基本参数:7 y; I+ z3 v. ^7 g& K4 R
jta | thread | managed | custom.Class
1 l! ]; L) m# O# P4 w3 C7 x1 N主要是配合
, C/ r0 R' Q# D1 D% u; Q1 hSessionFactory.getCurrentSession()
! S7 d5 B8 \: v. K来用的.$ t2 M$ U0 n6 d, f9 N; f
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ `5 ^" {, g: k# J* e
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! M% Q' }, X- G3 S6 n; Rcontext.BeginRequest += OpenSession;
, M4 h# H+ _& c, F.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 B, e& r* l2 S2 u8 o6 n: x; u
5 q, q5 _1 ^' A3 r2 t9 `* V//do transaction
8 [# t. ~3 w0 D) O$ B8 PgetCurrentSession().Submit();6 o2 s8 b+ M Y# X+ |
getCurrentSession().RollBack();/ C$ L; i9 r, P8 M. h
...) P% g/ g1 w" M/ P1 h' y. r, g
Unbind(HttpContext.Current, SessionFactory)
3 E: P1 T% _" m8 b6 C: {" j7 Q ?* q/ r; q" T4 ^
context.EndRequest += CloseSession;! p6 X* V- g' \! C2 j6 T
4 ] K z0 V- B# ?+ c" A$ h! _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|