 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 B1 @- w; W: t1 b
基本参数:( ?3 [ e$ I w$ d) O' [
jta | thread | managed | custom.Class
6 X D1 g: }4 p, w- o主要是配合
n4 v* R' _# I6 N% U# lSessionFactory.getCurrentSession()
4 M* a+ q+ R8 @' M% I# b$ s来用的." h, Y! C' k' F8 j3 W
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 }, _9 j+ f; w1 i0 c( EWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& i3 n$ ^1 R% F# lcontext.BeginRequest += OpenSession;
9 p3 v% y8 \: S2 T.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 H0 n) F1 Y. P8 a( P4 b
1 g) J* \( {& i: O//do transaction& ?1 J& `0 w$ N, e$ z
getCurrentSession().Submit();# X$ `# p, z( z* U7 o
getCurrentSession().RollBack();
5 _* E W3 t* Q! S...( b8 l" ?. E* p
Unbind(HttpContext.Current, SessionFactory)1 p2 Y4 A+ I4 J% X l. b
3 n+ t9 x) b8 p, pcontext.EndRequest += CloseSession;
4 `* s6 P) T8 P6 R/ Q1 |: J
r A+ H- L9 [0 Z# \% I 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|