 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 U5 [* I& [# l. x
基本参数:
2 b/ W+ V' _8 g$ }jta | thread | managed | custom.Class# Q" P ^% P, d
主要是配合5 z2 F, a% ?2 D
SessionFactory.getCurrentSession()
! \/ w2 W* x' J来用的.. Z( ?3 a; c* n2 I5 F9 y* [ ^
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ d% K" Y9 P) x. y3 J' g1 U3 X3 mWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:! f7 I; ]! ?3 P" X" d9 u
context.BeginRequest += OpenSession; {% d6 z2 ?" d2 M
.Bind(HttpContext.Current, SessionFactory.OpenSession())2 N$ D* d+ A5 _ Y$ o1 K2 n
/ ]7 r* }6 t" z" `
//do transaction! J/ h% W; k& U$ m/ J- ?8 k v, ^! j
getCurrentSession().Submit();( c ~: n5 l$ d1 y4 d6 Q
getCurrentSession().RollBack();9 H" O9 p* |7 [/ z9 ^* A
...
1 a b2 F# |0 J* I- QUnbind(HttpContext.Current, SessionFactory)
8 C8 [5 }. r- t) H1 i9 r% Q. F" t( |
context.EndRequest += CloseSession;
0 t3 X. m% o# x) n! Z8 A$ c! I9 J; {
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|