Error - The transaction associated with the current connection has completed, but has not been deleted.

I'm having trouble using TransactionScope to transfer multiple database queries into a transaction. I am using SqlBulkCopy with batchsize 500. When I increase the batch size to 1000, I get an error message:

The transaction associated with the current connection has been completed but has not been liquidated. The transaction must be deleted before the connection can be used to execute SQL statements.

This is the code I'm 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(); } } 
+44
sql-server-2008
Jul 12 2018-12-12T00:
source share
3 answers

I know this is late, but maybe it will help someone else.

This can happen when the transaction expires. You can increase the timeout for your transaction like this (use values ​​corresponding to the expected length of your transaction). Code below for 15 minutes:

 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(0, 15, 0))) { // working code here } 

That's why it could work on batchsize 500, not 1000.

+80
Jan 29 '13 at 16:35
source share

I found that setting a timeout in TransactionScope does not work for me. I also needed to add the following configuration key to the end of the machine.config <configuration> in order to skip the default maximum timeout for 10 minutes.

 <system.transactions> <machineSettings maxTimeout="00:30:00" /> <!-- 30 minutes --> </system.transactions> 

Credit: http://thecodesaysitall.blogspot.com.au/2012/04/long-running-systemtransactions.html

+15
Jan 22 '14 at 5:25
source share

Move scope.Complete(); outside the connection block.

 using (var scope = new TransactionScope()) { using (var connection = (SqlConnection)customerTable.OpenConnection()) { // } scope.Complete(); } 
+7
Jul 12 2018-12-12T00:
source share



All Articles