为什么有时我会收到“无效的交易对象” 例外?
这段代码有问题吗?
有时我会遇到未处理的“无效事务对象”异常:
procedure BlaBla;
var
TD: TDBXTransaction;
begin
TD := SQLConnection.BeginTransaction;
try
SQLConnection.ExecuteDirect('some sql command');
SQLConnection.ExecuteDirect('some sql command');
SQLConnection.CommitFreeAndNil(TD);
except
SQLConnection.RollbackFreeAndNil(TD);
end;
end;
此异常正在向用户引发,因此我假设它是由 RollbackFreeAndNil 引发的,因为所有其余部分都在 try.. except 内。
我应该用另一个 try.. except 包装 RollbackFreeAndNil 吗? 真是一团糟。
我正在使用 Delphi 2009、带有 Firebird 2.1 的 DBX 和 Devart 的驱动程序。
Is there something wrong with this code?
Sometimes I get an unhandled "Invalid transaction object" exception in it:
procedure BlaBla;
var
TD: TDBXTransaction;
begin
TD := SQLConnection.BeginTransaction;
try
SQLConnection.ExecuteDirect('some sql command');
SQLConnection.ExecuteDirect('some sql command');
SQLConnection.CommitFreeAndNil(TD);
except
SQLConnection.RollbackFreeAndNil(TD);
end;
end;
This exception is being raised to the user, so I assume it's raised by RollbackFreeAndNil, since all rest is inside a try..except.
Should I wrap RollbackFreeAndNil with another try..except? What a mess.
I'm using Delphi 2009, DBX with Firebird 2.1 and Devart's driver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 CommitFreeAndNil 抛出异常会发生什么?
RollbackFreeAndNil 将被调用。 那么TD还有效吗?
你正在吃掉例外,因此也就吃掉了证据。 不要那样做; 重新抛出:
What would happen if CommitFreeAndNil threw an exception?
RollbackFreeAndNil would be called. Would TD be valid then?
You're eating the exception, and hence the evidence. Don't do that; re-throw:
问题是,如果 SQLConnection 未连接到数据库,则 SQLConnection.BeginTransaction 返回 nil。 然后我得到无效交易对象的异常。
我从没想过会这样。 它应该尝试连接或引发异常。 返回零对我来说没有意义。
The problem is that SQLConnection.BeginTransaction returns nil if SQLConnection is not Connected to the database. And then I get the exception of invalid transaction object.
I never expected that. It should try to connect or raise an exception. Returning nil doesn't make sense to me.
有时,您使用 cxdbmemo 之类的组件执行各种过程,并给出错误。 您必须删除该组件(或类似的东西)并正常进行交易。
some times is that you execute varius procedures with a component like cxdbmemo and give you thats error. You have to remove that component (or somthing like this) and do your transaction normal.