 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" a6 s4 q8 ]) r: T基本参数:: Z4 |% K0 B9 e' V) V; r& |
jta | thread | managed | custom.Class
8 b8 M+ d3 F1 ^, j( t! m主要是配合
/ @2 N6 a/ q: I! Q* ESessionFactory.getCurrentSession()
) o* J7 I$ S0 X }5 {& P来用的.; O" ] d3 w7 A. h$ r- n6 p
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: o) _0 _( n( l7 O/ H5 _4 f
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 }, d0 n2 Y' K5 c2 l1 ?context.BeginRequest += OpenSession;. C+ q0 V: ~% n" l# |* c2 H
.Bind(HttpContext.Current, SessionFactory.OpenSession())) L: p# E y% z6 S1 z0 M6 y
+ G9 S: b$ z# O3 T4 O
//do transaction
7 z" E' \) X) e7 L. q! AgetCurrentSession().Submit();6 |8 `' v6 |5 @0 G$ h- S
getCurrentSession().RollBack();4 c. B; F8 ~4 o/ `+ z2 M
...
4 q6 O* Q' Z9 `! q* ?! j5 ^Unbind(HttpContext.Current, SessionFactory)( Q! P' f: b/ `& |& m, ^7 Q) \
9 L7 e4 n8 ^& C) y1 g, X: s
context.EndRequest += CloseSession;6 w* C7 C. T2 Z2 U' u% R5 h0 S- O/ `
; T/ `9 J) C% x7 U0 \: z, O. F" n
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|