 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' M1 U1 }% } H
基本参数:7 _6 G8 S7 W/ Y9 t _0 u2 u3 C
jta | thread | managed | custom.Class1 V: y1 O, d4 M! ]9 z
主要是配合
' y2 u- @$ m# m7 D( o2 ~4 k2 WSessionFactory.getCurrentSession()
/ e# c, \. i" w( }% P2 q来用的.1 z4 G- s5 c0 _% { d! ?6 y; M( p
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,0 p" H) H$ ^7 q
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ p: R# O. ? q+ s. j% B8 J) b
context.BeginRequest += OpenSession;2 _4 j6 B# }3 f) b& r, x( H5 ~
.Bind(HttpContext.Current, SessionFactory.OpenSession())4 u* R- r9 U: U1 I. |
( Q. e& E4 X0 l* Q, G H
//do transaction
5 @4 d$ u3 p; j, i# `getCurrentSession().Submit();
8 @% [+ D: O9 a5 j( {/ X+ q! MgetCurrentSession().RollBack();9 R) p m% b# u/ Y
...
7 ]0 s8 d( E2 S/ `: e5 Z0 fUnbind(HttpContext.Current, SessionFactory)
* p$ M5 Z; \9 }4 ?
% L/ j5 a4 g, A( w# ^0 y5 }context.EndRequest += CloseSession;
2 Z/ }6 F/ M% l& S; P% V }8 G7 Q" u/ ?* O9 q h
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|