 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* t9 H! E/ s3 w7 p$ G; [, _3 I基本参数:7 N" u w: d0 P9 \# t# j
jta | thread | managed | custom.Class
% C3 d Z& @" z2 a" H主要是配合
! \9 r* [& e; ^* ?' r# e5 qSessionFactory.getCurrentSession() g% `2 f$ k. D) |) e, s: E
来用的.
/ T- ?& M# d" O- s9 X% NThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 S4 D# M; |" O7 o9 f' F8 w' h2 _2 g
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 y C& A5 a7 @0 A% L! M7 ^. f
context.BeginRequest += OpenSession;
$ [/ f) S4 [/ M+ p) k4 S.Bind(HttpContext.Current, SessionFactory.OpenSession()): n+ c: F, D+ ?6 v
) ?# `. h; B9 e. k5 \8 a
//do transaction, m1 a6 M9 m1 r7 O6 w3 E
getCurrentSession().Submit();# O( _0 d; m* T8 j8 S9 e/ G
getCurrentSession().RollBack();% T9 |5 A( Q7 ^; m
...
- C! ]8 S- X' T3 u! v" x% g' y EUnbind(HttpContext.Current, SessionFactory)
0 f0 W+ i6 g) w H1 \, X; w
1 U9 g7 W7 z0 z# ^5 g2 t% lcontext.EndRequest += CloseSession;
# K1 l0 p* M! |4 Z: m- G# W
+ L2 `4 `- Z) i5 S. d6 [ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|