 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- [2 O8 {* i4 z基本参数:2 T7 b; q+ R* j6 P, c$ }3 k4 G5 S4 A; l
jta | thread | managed | custom.Class
) R# G; ]/ Y/ B# {9 l主要是配合
d h, O1 s- H' uSessionFactory.getCurrentSession()
) M& s- g) ^% h来用的.9 T: Y5 f F9 Y3 {6 V
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 i; t6 k7 C: ^ t& O/ g) iWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 ~" I) ?& v' K- T* o- n* [
context.BeginRequest += OpenSession;
) X9 [! ~( h1 m: | [ l! R2 \.Bind(HttpContext.Current, SessionFactory.OpenSession())4 d) u* v( j; w( h) }
5 {& m. w t; P O! J
//do transaction
1 g; @6 D2 l; ^% _" _getCurrentSession().Submit();
$ }6 O/ ]) _; J M4 d; ?getCurrentSession().RollBack();
5 O1 A. z/ y5 t* v2 M$ h- `4 t2 Y...+ L& h0 o& Q" X* d2 H( [: F; @
Unbind(HttpContext.Current, SessionFactory)
( @* e; M; `' T: C+ m C* N7 D6 Z$ g6 w- j4 Y2 i' n
context.EndRequest += CloseSession;" a9 i/ t/ A5 w, Y E3 g' _
B' E# I! t: a4 f
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|