gpt4 book ai didi

c# - 无法执行事务操作,因为有待处理的请求在工作

转载 作者:可可西里 更新时间:2023-11-01 07:44:16 29 4
gpt4 key购买 nike

背景

我有一些代码可以打开一个 sql 连接,开始一个事务并对数据库执行一些操作。此代码从数据库创建一个对象(出队),获取一些值并将其保存回来。整个操作需要在事务中进行。所有代码在没有事务的情况下都能完美运行。

using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var transaction = connection.BeginTransaction();
try
{
var myObject = foo.Dequeue(connection, transaction);

var url = myObj.GetFilePathUri(connection, transaction);

//some other code that sets object values

myObj.SaveMessage(connection, transaction);
transaction.Commit(); //error here
}
catch(Exception ex)
{
transaction.Rollback();
//logging
}
finally
{
//cleanup code
}
}

dequeue方法代码

public foo Dequeue(SqlConnection connection, SqlTransaction transaction)
{
using (var command = new SqlCommand(DEQUEUE_SPROC, connection) {CommandType = CommandType.StoredProcedure, Transaction = transaction})
{
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
ID = (Guid) reader["ID"];
Name = reader["Name"].ToString();
return this;
}
return null;
}
}

获取路径代码

public string GetFilePathUri(SqlConnection connection, SqlTransaction transaction)
{
using (var command = new SqlCommand(FILEPATH_SPROC, connection) {CommandType = CommandType.StoredProcedure, Transaction = transaction})
{
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
return reader["Path"].ToString();
}
return "";
}
}

保存代码

public void SaveMessage(SqlConnection connection, SqlTransaction transaction)
{
using (var command = new SqlCommand(SAVE_SPROC, connection) {CommandType = CommandType.StoredProcedure, Transaction = transaction})
{
command.Parameters.Add("@ID", SqlDbType.UniqueIdentifier).Value = ID;
command.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
//other object params here
command.ExecuteNonQuery();
}
}

问题

调用 transaction.Commit() 时,出现以下错误:

The transaction operation cannot be performed because there are pending requests working on this transaction.

我做错了什么?

编辑:快速编辑说我已经在 SO 上阅读了关于这个问题的其他问题,但找不到任何与 ADO.net 相关的内容

最佳答案

我以前遇到过这个问题,问题是需要关闭阅读器。试试这个:

public foo Dequeue(SqlConnection connection, SqlTransaction transaction)
{
using (var command = new SqlCommand(DEQUEUE_SPROC, connection) {CommandType = CommandType.StoredProcedure, Transaction = transaction})
{
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
ID = (Guid) reader["ID"];
Name = reader["Name"].ToString();
reader.Close();//Closing the reader
return this;
}
return null;
}
}


public string GetFilePathUri(SqlConnection connection, SqlTransaction transaction)
{
string filePathUri = "";
using (var command = new SqlCommand(FILEPATH_SPROC, connection) {CommandType = CommandType.StoredProcedure, Transaction = transaction})
{
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
filePathUri = reader["Path"].ToString();
}
reader.Close();//Closing the reader
}
return filePathUri;
}

关于c# - 无法执行事务操作,因为有待处理的请求在工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552285/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com