title: TransactionInDoubtException using System.Transactions on SQL Server 2005
body: <p>The underlying question to this post is "Why would a non-promoted LTM Transaction ever be in doubt?" </p>
  
  <p>I'm getting System.Transactions.TransactionInDoubtException and i can't explain why. Unfortunately i cannot reproduce this issue but according to trace files it does happen. I am using SQL 2005, connecting to one database and using one SQLConnection so i don't expect promotion to take place. The error message indicates a timeout. However, sometimes I get a timeout message but the exception is that the transaction has aborted as opposed to in doubt, which is much easier to handle.</p>
  
  <p>Here is the full stack trace:</p>
  
  <pre>System.Transactions.TransactionInDoubtException: The transaction is in doubt. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
     at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
     at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
     at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
     at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
     at System.Data.SqlClient.TdsParserStateObject.ReadByte()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
     at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
     at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
     at System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment)
     --- End of inner exception stack trace ---
     at System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx)
     at System.Transactions.CommittableTransaction.Commit()
     at System.Transactions.TransactionScope.InternalDispose()
     at System.Transactions.TransactionScope.Dispose()
  </pre>
  
  <p>Any ideas? Why am i getting in doubpt and what should i do when i get it?</p>
  
  <p><strong>EDIT for more information</strong></p>
  
  <p>I actually still don't have the answer for this. What I did realize is that the transaction actually partially commits. One table gets the insert but the other does not get the update. The code is HEAVILY traced and there is not much room for me to be missing something. </p>
  
  <p>Is there a way I can easily find out if the transaction has been promoted. Can we tell from the stack trace if it is? SIngle Phase commit (which is in the strack trace) seems to indicate no promotion to me, but maybe i'm missing something. If its not getting promoted then how can it be in doubt. </p>
  
  <p>Another interesting piece to the puzzle is that i create a clone of the current transaction. I do that as a workarround to this issue. 
  <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=914869&amp;SiteID=1" rel="noreferrer">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=914869&amp;SiteID=1</a></p>
  
  <p>Unfortunately, i don't know if this issue has been resolved. Maybe creating the clone is causing a problem. Here is the relevant code</p>
  
  <pre><code>using (TransactionScope ts = new TransactionScope())
  {
     transactionCreated = true;
     //part of the workarround for microsoft defect mentioned in the beginning of this class
     Transaction txClone = Transaction.Current.Clone();
     transactions[txClone] = txClone;
     Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(TransactionCompleted);
     MyTrace.WriteLine("Transaction clone stored and attached to event");
  
     m_dataProvider.PersistPackage(ControllerID, package);
     MyTrace.WriteLine("Package persisted");
     m_dataProvider.PersistTransmissionControllerStatus(this);
     MyTrace.WriteLine("Transmission controlled updated");
     ts.Complete();
  }
  </code></pre>
  
  <p>Thanks</p>