 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. K2 A; c. P9 q% S6 S( |基本参数:. f: E6 G/ f- k: Y# l, Z: o
jta | thread | managed | custom.Class
8 i7 `) \% w+ g4 F% S: ]主要是配合9 c4 v9 `" R; K9 {& X/ e" V
SessionFactory.getCurrentSession()
) a) I7 q7 o9 B% P来用的.9 Y( }4 D3 h- t2 t. {
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 j6 ?% ]7 x; ~1 V0 pWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 I) C0 R) U: Y% o: }% ocontext.BeginRequest += OpenSession;+ Y' R9 d" O) V' } G- ]
.Bind(HttpContext.Current, SessionFactory.OpenSession())
, a* Z2 F5 k" |
7 D: a! q& z( L& S+ C5 c1 _: ?//do transaction
4 O$ ]) J q) q. dgetCurrentSession().Submit();: {+ U. \ ~) a
getCurrentSession().RollBack();/ ~- `3 Q; N! v) ?
...
# U* p ~4 n( P6 CUnbind(HttpContext.Current, SessionFactory)7 E$ t- u# B# c1 H* `
7 K& m, Z$ U" i% E( \; u$ gcontext.EndRequest += CloseSession;+ Q5 k+ @5 K! O& |! U2 r' |2 q* r
+ v) N8 K! z! F! Q' ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|