Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:51:01+00:00 2026-05-28T06:51:01+00:00

We are having an issue when using NHibernate with distributed transactions. Consider the following

  • 0

We are having an issue when using NHibernate with distributed transactions.

Consider the following snippet:

//
// There is already an ambient distributed transaction
//
using(var scope = new TransactionScope()) {
    using(var session = _sessionFactory.OpenSession())
    using(session.BeginTransaction()) {
        using(var cmd = new SqlCommand(_simpleUpdateQuery, (SqlConnection)session.Connection)) {
            cmd.ExecuteNonQuery();
        }

        session.Save(new SomeEntity());
        session.Transaction.Commit();
    }
    scope.Complete();
}

Sometimes, when the server is under extreme load, we’ll see the following:

  1. The query executed with cmd.ExecuteNonQuery is chosen as a deadlock victim (we can see it in SQL Profiler), but no exception is raised.
  2. session.Save fails with the error message, “The operation is not valid for the state of the transaction.”
  3. Every time this code is executed after that, session.BeginTransaction fails. The first few times, the inner exception varies (sometimes it is the deadlock exception that should have been raised in step 1). Eventually it stabilizes to “The server failed to resume the transaction. Desc:3800000177.” or “New request is not allowed to start because it should come with valid transaction descriptor.”

If left alone, the application will eventually (after seconds or minutes) recover from this condition.

Why is the deadlock exception not being reported in step 1? And if we can’t resolve that, then how can we prevent our application from temporarily becoming unusable?

The issue has been reproduced in the following environments

  • Windows 7 x64 and Windows Server 2003 x86
  • SQL Server 2005 and 2008
  • .NET 4.0 and 3.5
  • NHibernate 3.2, 3.1 and 2.1.2

I’ve created a test fixture which will sometimes reproduce the issue for us. It is available here: http://wikiupload.com/EWJIGAECG9SQDMZ

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:51:01+00:00Added an answer on May 28, 2026 at 6:51 am

    We’ve finally narrowed this down to a cause.

    When opening a session, if there is an ambient distributed transaction, NHibernate attaches an event handler to the Transaction.TransactionCompleted, which closes the session when the distributed transaction is completed. This appears to be subject to a race condition wherein the connection may be closed and returned to the pool before the deadlock error propagates across, leaving the connection in an unusable state.

    The following code will reproduce the error for us occasionally, even without any load on the server. If there is extreme load on the server, it becomes more consistent.

    using(var scope = new TransactionScope()) {
        //
        // Force promotion to distributed transaction
        //
        TransactionInterop.GetTransmitterPropagationToken(Transaction.Current);
    
        var connection = new SqlConnection(_connectionString);
        connection.Open();
    
        //
        // Close the connection once the distributed transaction is
        // completed.
        //
        Transaction.Current.TransactionCompleted += 
            (sender, e) => connection.Close();
    
        using(connection.BeginTransaction())
            //
            // Deadlocks but sometimes does not raise exception
            //
            ForceDeadlockOnConnection(connection);
    
        scope.Complete();
    }
    
    //
    // Subsequent attempts to open a connection with the same
    // connection string will fail
    //
    

    We have not settled on a solution, but the following things will eliminate the problem (while possibly having other consequences):

    • Turning off connection pooling
    • Using NHibernate’s AdoNetTransactionFactory instead of AdoNetWithDistributedTransactionFactory
    • Adding error handling that calls SqlConnection.ClearPool() when the “server failed to resume the transaction” error occurs

    According to Microsoft (https://connect.microsoft.com/VisualStudio/feedback/details/722659/), the SqlConnection class is not thread-safe, and that includes closing the connection on a separate thread. Based on this response we have filed a bug report for NHibernate (http://nhibernate.jira.com/browse/NH-3023).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using NHibernate 2.1.2.400, and I'm having an issue with a an ISQLQuery query.
I believe I am having a memory issue using numpy arrays. The following code
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
I am having an issue with many-to-many mapping using NHibernate. Basically I have 2
I am using Fluent NHibernate and having some issues getting a many to many
I am having a issue using Google's SDC (Secure Data Connector) with my Google
I'm having an issue using the Cookie class of the Servlet API 2.5 on
i'm having a little issue with doctrine using symfony 1.4 (I think it's using
I'm having an issue verifying if a checkbox is checked using jquery on Internet
I'm having an issue in IE 6 and 7 when using jqGrid (v3.6.4) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.