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

  • SEARCH
  • Home
  • 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 5979221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:37:44+00:00 2026-05-22T21:37:44+00:00

I have an application running multiple threads. The threads do NOT share an ObjectContext

  • 0

I have an application running multiple threads. The threads do NOT share an ObjectContext (each thread has its own – I know they are not thread safe).

However, the threads are all operating under a shared Transaction. The original thread creates a TransactionScope and each thread it spawns creates a TransactionScope using a DependentTransaction from the Transaction on the main thread.

When multiple ObjectContext requests run at the same time, I sometimes (not consistently) get the error:

System.Data.EntityException occurred
  Message=An error occurred while closing the provider connection. See the inner exception for details.

  InnerException: System.Transactions.TransactionException
       Message=The operation is not valid for the state of the transaction.
       Source=System.Transactions
       StackTrace:
            at System.Transactions.TransactionStatePSPEOperation.get_Status(InternalTransaction tx)
            at System.Transactions.TransactionInformation.get_Status()
            at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
            at System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
            at System.Data.SqlClient.SqlConnection.Close()
            at System.Data.EntityClient.EntityConnection.StoreCloseHelper()
       InnerException: 

I only know they are running at the same time because when I run my unit tests in debug mode and this exception pops up, if I look at the different threads that are running, I always see at least one other thread halted at an ObjectContext operation.

Also, after doing some reading, I tried adding multipleactiveresultsets=False to my connection string and that made no difference.

Is this a bug in Entity Framework?

  • 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-22T21:37:45+00:00Added an answer on May 22, 2026 at 9:37 pm

    The problem is described here:

    http://www.b10g.dk/2007/09/07/dependenttransaction-and-multithreading/

    It’s easy enough to lock the SaveChanges and Refresh calls, but in order to make sure locks occur during query execution I had to make a dummy query provider that locks when executing queries. I really shouldn’t have had to do this. Entity Framework should have been robust enough to handle this out of the box…especially considering you’re not meant to handle your own connection creation.

    Here is the code for the query provider wrapper. The IQueryables themselves and the base QueryProvider class are simple reusable implementations based off here
    http://blogs.msdn.com/b/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx

        /// <summary>
        /// A wrapper for queries executed by EF.
        /// </summary>
        internal class EntityFrameworkQueryProvider : QueryProvider
        {    
            protected override object Execute(Expression expression)
            {
                try
                {
                    // this is required due to a bug in how EF multi-threads when Transactions are used.
                    if (Transaction.Current != null) Monitor.Enter(EntityFrameworkExtensions.SyncRoot);
    
                    // enumerate is a simple extension method that forces enumeration of the IQueryable, thus making it actually get executed during the lock
                    return Expression.Lambda(expression).Compile().DynamicInvoke().Enumerate();
                }
                finally
                {
                    if (Transaction.Current != null) Monitor.Exit(EntityFrameworkRepositoryProvider.SyncRoot);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has multiple threads processing work from a todo queue.
Here's the scenario: I have a multi threaded java web application which is running
I have a media application (written in Delphi 2010 but I am not sure
We have a web application running on Tomcat 7 and it loads a JNI
I have a 32 bit msvc++ application running in a production environment that is
There is a Java Struts application running on Tomcat, that have some memory errors.
As the title indicates I have an MVC 2 Application running on Server 2003.
Is fprintf thread-safe? The glibc manual seems to say it is, but my application,
I have several processes running concurrently that I want to log to the same
I get: Your project contains error(s), please fix them before running your application. ...when

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.