为什么有时我会收到“无效的交易对象” 例外?

发布于 2024-07-10 14:21:44 字数 586 浏览 10 评论 0原文

这段代码有问题吗?
有时我会遇到未处理的“无效事务对象”异常:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2024-07-17 14:21:44

如果 CommitFreeAndNil 抛出异常会发生什么?

RollbackFreeAndNil 将被调用。 那么TD还有效吗?

你正在吃掉例外,因此也就吃掉了证据。 不要那样做; 重新抛出:

procedure BlaBla;
var
  TD: TDBXTransaction;
begin
  TD := SQLConnection.BeginTransaction;
  try
    SQLConnection.ExecuteDirect('some sql command');
    SQLConnection.ExecuteDirect('some sql command');
  except
    SQLConnection.RollbackFreeAndNil(TD);
    raise;
  end;
  SQLConnection.CommitFreeAndNil(TD);
end;

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:

procedure BlaBla;
var
  TD: TDBXTransaction;
begin
  TD := SQLConnection.BeginTransaction;
  try
    SQLConnection.ExecuteDirect('some sql command');
    SQLConnection.ExecuteDirect('some sql command');
  except
    SQLConnection.RollbackFreeAndNil(TD);
    raise;
  end;
  SQLConnection.CommitFreeAndNil(TD);
end;
和影子一齐双人舞 2024-07-17 14:21:44

问题是,如果 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.

送舟行 2024-07-17 14:21:44

有时,您使用 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.

我很坚强 2024-07-17 14:21:44
SQLConnection.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted);
SQLConnection.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文