 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" I; } P6 e$ H% [$ u基本参数:! }7 Y) ?; Y# e- e7 ~9 N$ v! O
jta | thread | managed | custom.Class
) {6 j( K$ i7 ^. M5 w9 L, t主要是配合 X6 z6 k) {& l3 G7 V/ r, |/ v0 P
SessionFactory.getCurrentSession()
7 j T6 c: S# f& U来用的.. F* j3 B; \) p" v
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; B* d! v" K. u8 B1 D- M bWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& g' P* L5 |+ Econtext.BeginRequest += OpenSession;# R1 H2 a# e- P+ _" S6 u: j: d! T
.Bind(HttpContext.Current, SessionFactory.OpenSession())
% X2 V7 F* K& d. \
, o$ z) U( N5 c! S1 {' B//do transaction; |1 V$ W5 g. e- b
getCurrentSession().Submit();
; R( I: H: |8 u6 b, _* _getCurrentSession().RollBack();, K+ T4 g3 `: I0 R0 h$ G
...& L8 A: U/ J/ n8 [2 D
Unbind(HttpContext.Current, SessionFactory)
# D) j, G8 Z# k& g+ a. W
) ]0 A2 ^0 ?2 q) }4 O' |5 Wcontext.EndRequest += CloseSession;" T8 I7 G( S/ w) n. |8 e
n. V0 E8 m# k) A+ J" C, ? 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|