 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) g. s/ m: k' j* P/ P+ X
基本参数:5 m$ v9 ~ w y" u9 G# b6 A
jta | thread | managed | custom.Class) i. l+ \0 E, A( D. e2 K
主要是配合& j( l. W- S5 a3 W5 U
SessionFactory.getCurrentSession()
5 F7 _3 z% r7 k来用的.
; l* a0 a7 K3 y0 w0 f/ c; G {6 ~Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
& o7 m. z+ n, ]1 dWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- P) Y5 q$ W& x4 v# Ycontext.BeginRequest += OpenSession;
! t& n6 |& ?0 u+ Q C.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 g* C" k8 ~% Y" \1 [' y6 w8 A, }) k P& q* e
//do transaction
7 N$ \3 b9 i6 Q( r# Y4 R* f% G0 ygetCurrentSession().Submit();
# u0 [2 ^! E; c3 bgetCurrentSession().RollBack();+ \1 i! m5 T# |
...( Z$ p( I$ | ^& g
Unbind(HttpContext.Current, SessionFactory)
+ g }8 ^/ u. B# N* o" h! C+ d; L; ^
context.EndRequest += CloseSession;
: [* R. A: J' V1 G. C8 O
% N: i) f# K- n% G 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|