COMMIT TRANSACTION请求没有对应的BEGIN TRANSACTION

发布于 2024-10-31 17:57:19 字数 566 浏览 1 评论 0 原文

这是我在生产服务器上遇到的一个奇怪的问题。过去两周内发生了两次这种情况,并且该服务器的流量很大。

我们在 Web 服务中有一些代码执行 BEGIN TRAN,然后运行一些 SQL 查询(两次插入,然后更新)。然后最后执行COMMIT。现在我们已经在日志中收到两次消息:

COMMIT TRANSACTION 请求没有对应的 BEGIN TRANSACTION。

在前两次插入和更新之间,我们调用另一个 Web 服务,因此在调用 COMMIT 之前,前两次插入和最后一次更新之间可能会有轻微的延迟。这会导致我们的问题吗?我们在 IIS 7 和 Server 2008 R2 上运行此程序(已应用所有更新)。

最初我们认为这可能是应用程序池被回收,但将其更改为在半夜回收。现在我不确定什么会导致 SQL Server 忘记对 BEGIN TRAN 的调用。

这个网络服务确实被频繁调用。有人见过这样的事情吗?我现在完全不知所措......

非常感谢任何帮助或建议!

Here's a strange problem I'm running into on a production server. It has happened twice in the last two weeks, and this is a server that gets a lot of traffic.

We have some code in a Web Service that executes a BEGIN TRAN, then runs a few SQL queries (two inserts followed by an update). Then at the end executes a COMMIT. Twice now we have gotten the message in the logs:

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

Between the first two inserts and the update, we call another web service, so there could be a slight delay between the first two inserts and last update before the COMMIT is called. Could this be causing our problem? We're running this on IIS 7 and Server 2008 R2 (all updated applied).

Originally we though it could be the app pools getting recycled, but changed that to recycle in the middle of the night. Now I'm not sure what would be causing SQL server to forget the call to BEGIN TRAN.

This web service does get called quite a bit. Has anyone seen something like this before? I'm at a total loss at the moment...

Any help or suggestion appreciated greatly!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

-小熊_ 2024-11-07 17:57:19

看起来你的事务失败了,被回滚了,没有什么可以提交的

例子

CREATE TABLE BlaTest(id INT PRIMARY KEY NOT NULL)
GO

现在运行这个

BEGIN TRAN

INSERT BlaTest VALUES('a')
GO

COMMIT TRAN

这是错误

Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value 'a' to data type int.
Msg 3902, Level 16, State 1, Line 2
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

这将运行没有问题

BEGIN TRAN

INSERT BlaTest VALUES(5)
GO

COMMIT TRAN

关于事务的好文章是SQL 2005 及更高版本中的错误处理 作者:Erland Sommarskog

It looks like your transaction failed, got rolled back and there is nothing to commit

example of such a thing

CREATE TABLE BlaTest(id INT PRIMARY KEY NOT NULL)
GO

Now run this

BEGIN TRAN

INSERT BlaTest VALUES('a')
GO

COMMIT TRAN

Here is the error

Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value 'a' to data type int.
Msg 3902, Level 16, State 1, Line 2
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

This will run without a problem

BEGIN TRAN

INSERT BlaTest VALUES(5)
GO

COMMIT TRAN

A good article on transactions is Error Handling in SQL 2005 and Later by Erland Sommarskog

带上头具痛哭 2024-11-07 17:57:19

我的问题是我需要一个 BEGIN TRAN 和 COMMIT TRAN 周围的 BEGIN 和 END 。

BEGIN
     BEGIN TRAN

     INSERT BlaTest VALUES(5)
     GO

     COMMIT TRAN
END

My issue was I needed a BEGIN and END around my BEGIN TRAN and COMMIT TRAN.

BEGIN
     BEGIN TRAN

     INSERT BlaTest VALUES(5)
     GO

     COMMIT TRAN
END
離人涙 2024-11-07 17:57:19

BEGIN TRANS会有帮助

顶部的

BEGIN TRANS

at the top will help

英雄似剑 2024-11-07 17:57:19

我也有这个问题。

结果,就我而言,问题是两个 提交

第一个按预期工作。第二个产生了这个消息。删除第二个提交解决了问题。

I had this problem as well.

Turned out, in my case, the problem was two commits.

The first one worked as intended. The second produced this message. Deleting the second commit solved the problem.

浪荡不羁 2024-11-07 17:57:19

我有同样的问题。这就是我所做的来解决它。

COMMIT TRANSACTION 请求没有相应的 BEGIN TRANSACTION

在我检查 SQL 查询并添加 BEGIN TRAN 后,它将执行成功。这是我的示例代码。它将起作用:

ALTER procedure [dbo].[DeactivateUser]
    @UserId bigint,
    @LoginEmail Nvarchar(100),
    @merchantId int
    as
    Begin
      Begin tran

        update Users set 
        LoginEmail='inactive'+CONVERT(VARCHAR(11), getdate(), 106)+'-'+@LoginEmail,
        IsActive=0 
        where LoginEmail=@LoginEmail and MerchantID=@merchantId                     
        if(@@ERROR=0)
          begin
            commit Tran
            select 0
          end
        else
          begin
            rollback Tran
            select -1
          end


    end

I had the same issue. This is what I did to solve it.

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION

After I Checked the SQL Query and Add a BEGIN TRAN it will executed successfully. Here My sample code. It will work:

ALTER procedure [dbo].[DeactivateUser]
    @UserId bigint,
    @LoginEmail Nvarchar(100),
    @merchantId int
    as
    Begin
      Begin tran

        update Users set 
        LoginEmail='inactive'+CONVERT(VARCHAR(11), getdate(), 106)+'-'+@LoginEmail,
        IsActive=0 
        where LoginEmail=@LoginEmail and MerchantID=@merchantId                     
        if(@@ERROR=0)
          begin
            commit Tran
            select 0
          end
        else
          begin
            rollback Tran
            select -1
          end


    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文