Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql transaction failure

I am getting below error while inserting records in database.

System.Transactions.TransactionException: The operation is not valid for the state of the transaction. ---> System.TimeoutException: Transaction Timeout
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
   at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
   at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()

Actually i am inserting data in database by transaction scope method, code for which is described below.

TransactionOptions tOptions = new TransactionOptions();
tOptions.IsolationLevel = IsolationLevel.ReadCommitted;
tOptions.Timeout = TransactionManager.MaximumTimeout;
using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, tOptions))
{
}

Is there anything incorrect in above statement?

One thing to mention here is that i am bulk inserting data in atleast 10 tables with lot of records and tables also allow duplicate records to be inserted in bulk insert, Syntax used to achieve this is mentioned below.

CREATE UNIQUE INDEX Index_a ON table1([c1], [c2]) WITH IGNORE_DUP_KEY

Will be thankful if anyone could help me with this issue.

like image 538
Rajat Saini Avatar asked Apr 18 '13 20:04

Rajat Saini


1 Answers

The problem was that the MSDTC service was disabled and I was using bulk db operations. After enabling it the problem was resolved.

like image 75
Rajat Saini Avatar answered Sep 18 '22 19:09

Rajat Saini