C# 错误 - 与当前连接关联的事务已完成但尚未处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11453066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error - The transaction associated with the current connection has completed but has not been disposed
提问by Pradeep
I've been having trouble using the TransactionScopeto wrap multiple database queries into a transaction, I am using SqlBulkCopy with batchsize 500. When I increased the batch size to 1000 I am getting the error:
我在使用TransactionScope将多个数据库查询包装到一个事务中时遇到了问题,我使用的是 SqlBulkCopy 和批处理大小 500。当我将批处理大小增加到 1000 时,出现错误:
The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements.
与当前连接关联的事务已完成但尚未处理。必须先处理事务,然后才能使用连接执行 SQL 语句。
This is the code I am using:
这是我正在使用的代码:
using (var scope = new TransactionScope())
{
using (var connection = (SqlConnection)customerTable.OpenConnection())
{
var table1BulkCopy = new SqlBulkCopy(connection)
{
BatchSize = BATCH_SIZE,
DestinationTableName = TableName1
};
table1BulkCopy.WriteToServer(table1DataTable);
var table2BulkCopy = new SqlBulkCopy(connection)
{
BatchSize = BATCH_SIZE,
DestinationTableName = TableName2
};
table2BulkCopy.WriteToServer(table2DataTable);
var table3BulkCopy = new SqlBulkCopy(connection)
{
BatchSize = BATCH_SIZE,
DestinationTableName = TableName3
};
table1BulkCopy.WriteToServer(table3DataTable);
var table4BulkCopy = new SqlBulkCopy(connection)
{
BatchSize = BATCH_SIZE,
DestinationTableName = TableName4
};
table4BulkCopy.WriteToServer(table4DataTable);
scope.Complete();
}
}
回答by adatapost
Move scope.Complete();outside the connectionblock.
移动scope.Complete();外部connection块。
using (var scope = new TransactionScope())
{
using (var connection = (SqlConnection)customerTable.OpenConnection())
{
//
}
scope.Complete();
}
回答by Anthony Queen
This can happen when the transaction times out. You can increase the timeout for your transaction like this (use values appropriate for the expected length of your transaction). The code below is for 15 minutes:
当事务超时时可能会发生这种情况。您可以像这样增加交易的超时时间(使用适合交易预期长度的值)。下面的代码是 15 分钟:
using (TransactionScope scope =
new TransactionScope(TransactionScopeOption.Required,
new System.TimeSpan(0, 15, 0)))
{
// working code here
}
This is why it could have worked for batchsize 500 and not for 1000.
这就是为什么它可能适用于批量 500 而不适用于 1000。
回答by Adam
I found that setting the timeout in the TransactionScope didn't work for me. I also needed to add the following config key to the end of the machine.config <configuration>tag to extend past the default max timeout of 10 minutes.
我发现在 TransactionScope 中设置超时对我不起作用。我还需要将以下配置键添加到 machine.config<configuration>标记的末尾,以延长超过 10 分钟的默认最大超时时间。
<system.transactions>
<machineSettings maxTimeout="00:30:00" /> <!-- 30 minutes -->
</system.transactions>
Credit: http://thecodesaysitall.blogspot.com.au/2012/04/long-running-systemtransactions.html
信用:http: //thecodesaysitall.blogspot.com.au/2012/04/long-running-systemtransactions.html
回答by Alexander Shapkin
The full answer must be more full.
完整的答案必须更完整。
You must specify - where will be max transaction timeout determined - in the .Net code, or in the server config
您必须在 .Net 代码或服务器配置中指定 - 确定最大事务超时的位置
<sectionGroup name="system.transactions"....
...allowDefinition="MachineOnly"
</sectionGroup>
In this case you can set max timeout in the machine.config
在这种情况下,您可以在 machine.config 中设置最大超时时间
<configuration>
<system.transactions>
<machineSettings maxTimeout="01:00:00" />
</system.transactions>
</configuration>
Or may be you want to override this behaivor in the application. Then in the machine.config you should set atribute the velue:
或者您可能想在应用程序中覆盖此行为。然后在 machine.config 中,您应该设置属性:
...allowDefinition="MachineToApplication"
This is a good arcticle: https://blogs.msdn.microsoft.com/ajit/2008/06/18/override-the-system-transactions-default-timeout-of-10-minutes-in-the-code/
这是一篇很好的文章:https: //blogs.msdn.microsoft.com/ajit/2008/06/18/override-the-system-transactions-default-timeout-of-10-minutes-in-the-code/
回答by Vortman
Pretty obvious problem with time out, but you don't get effect if you set TransactionOptions.Timeout higher. Even if you set TimeSpan.MaxValue you don't actually get a profit. It doesnt matter that the Timeout property of the TransactionOptions are set to a higher value, TransactionOptions.Timeout can not exceed maxTimeout property. You should set up some changes in machine.config.
超时问题非常明显,但如果将 TransactionOptions.Timeout 设置得更高,则不会生效。即使您设置了 TimeSpan.MaxValue,您实际上也不会获得利润。将 TransactionOptions 的 Timeout 属性设置为更高的值并不重要,TransactionOptions.Timeout 不能超过 maxTimeout 属性。您应该在machine.config 中设置一些更改。
Shortly you should find machine.config file
%windir%\Microsoft.NET\Framework\yourversion\config\machine.config
And add this in <configuration>tag:
很快你应该找到 machine.config 文件 %windir%\Microsoft.NET\Framework\ yourversion\config\machine.config
并将其添加到<configuration>标签中:
<system.transactions>
<machineSettings maxTimeout="00:30:00"/>
</system.transactions>
Here you can set maxTimeout property to 30 minutes.
See following for more details http://thecodesaysitall.blogspot.com/2012/04/long-running-systemtransactions.html
您可以在此处将 maxTimeout 属性设置为 30 分钟。
有关更多详细信息,请参阅以下内容http://thecodesaysitall.blogspot.com/2012/04/long-running-systemtransactions.html

