 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what, `3 q$ G5 ?, S0 S4 V# Q; C; c$ n
基本参数:' I3 z$ Q6 V0 i: X
jta | thread | managed | custom.Class
' m7 z$ ^# R1 u/ c& Q. J. T主要是配合
$ X- l7 k# F: x1 b% l9 A/ f8 bSessionFactory.getCurrentSession()
- D5 G7 [$ G4 G& I) f来用的., ^9 J! k! j" @+ [( ^! [
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 m0 G m8 d, p& q$ ^) W
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 H; N, f/ H' {2 n9 L/ B8 c
context.BeginRequest += OpenSession;
2 G) x1 Y& C, J" D) o.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ K, L D( _) M7 A' w% ]. }
# F& m" U' F9 @# [. b C; A//do transaction8 a+ K6 V; s3 g0 T7 J% L9 O: m
getCurrentSession().Submit();
/ h! J8 g1 _3 i& M- f$ }getCurrentSession().RollBack();
* g' }! d" t( J8 A...; V, _" h! M4 g8 T- Z: x9 b) I) h
Unbind(HttpContext.Current, SessionFactory): x2 t9 H# d" j: @ w- J
* I* @% j; N: D8 @$ C1 d
context.EndRequest += CloseSession;
. F' l, e, g) o5 e8 s: {2 @0 d: J
9 |5 w( l% K/ z; j 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|