 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 O# {: t- G/ @) _* x
基本参数:, c9 A# e9 K7 Q' g3 u- {
jta | thread | managed | custom.Class) F' d6 v3 x1 B1 x, s
主要是配合0 P. Z! @& d- r6 I7 ~4 f5 J
SessionFactory.getCurrentSession()! ]/ Y6 J$ A& a" S
来用的.
* R1 p/ t3 ^3 O+ ?Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 n3 U* ]) d! a$ Y7 zWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:5 X$ p6 v5 B {. s% n' d+ A
context.BeginRequest += OpenSession;
4 F' ?. u& t: W. [.Bind(HttpContext.Current, SessionFactory.OpenSession())
' G7 ^2 i8 P: o/ u# e5 Q9 D2 N1 g
//do transaction
+ }, M3 K# ?6 q: `+ b( ygetCurrentSession().Submit();; l: }: u& N! e* \
getCurrentSession().RollBack();5 B% Q; U R' a ]
...% r( I2 H" P, E# y- @9 H
Unbind(HttpContext.Current, SessionFactory)
. F3 F0 o9 ^5 Y% N% x3 n. b7 \8 s. _& t4 Y: ~3 b+ P
context.EndRequest += CloseSession;
1 c8 J& `- Q/ C
, U7 C) S) y! {) |4 G% B& b- g2 D, k, V 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|