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 6153361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:04:53+00:00 2026-05-23T20:04:53+00:00

for synchronizing access to my NHibernate session at web environment I try use Mutex:

  • 0

for synchronizing access to my NHibernate session at web environment I try use Mutex:

public class FactoryRepository
{
    private FactoryRepository() { }
    private static Mutex _sessionMutex = new Mutex();
    private static ISessionFactory factory;
    public static ISessionFactory SessionFactory
    {
        get
        {
            factory = factory ?? new Configuration().Configure().BuildSessionFactory();
            return factory;
        }
    }

    public static ISession Session
    {
        get
        {
            ISession currentSession;
            _sessionMutex.WaitOne();
            if (HttpContext.Current != null)
            {
                HttpContext context = HttpContext.Current;
                currentSession = context.Items[SessionKey] as ISession;
                if (currentSession == null || !currentSession.IsOpen)
                {
                    currentSession = SessionFactory.OpenSession();
                    context.Items[SessionKey] = currentSession;
                }
            }

            _sessionMutex.ReleaseMutex();
            return currentSession;

        }
    }
}

At my error logging I get:

System.Threading.AbandonedMutexException: The wait completed due to an abandoned mutex.
Method: Boolean WaitOne(Int64, Boolean)

Stack Trace:

 at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext)
 at System.Threading.WaitHandle.WaitOne(Int32 millisecondsTimeout, Boolean exitContext)
 at System.Threading.WaitHandle.WaitOne()

Why do I get this exception with calling ReleaseMutex();

  • 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-23T20:04:54+00:00Added an answer on May 23, 2026 at 8:04 pm

    Your issue is on this line

     _sessionMutex.WaitOne();
    

    The WaitOne() can throw this exceptions because some other thread that lock it, exit with out releasing it

    In your case the WaitOne throw this exception because of an abandon of the same mutex in an other thread.

    I suggest to warp your mutex in a class and use a code like:

                try
                {
                    cLock = _sessionMutex.WaitOne();
                    // call your work
                }
                catch (AbandonedMutexException)
                {
                    cLock = true;
                    // call your work
                }
                catch (Exception x)
                {
                    //Error
                }
                finally
                {
                   _sessionMutex.ReleaseMutex();
                }
    

    In the above code the ReleaseMutex may fail to run if a user stop/abandon the page and the thread is lost/delete it. And thats why you get this exception.

    Be ware that mutex can lock for ever the way you do it ! 🙂 Its better to add a millisecond limit on the wait, and / or handle the case of non lock to return read only data. You users can lock for long time if the mutex fails to pass the WaitOne()

    Also be ware, the Mutex need to be close and dispose. ! Even if this is like the example in MSDN, in MSDN is only an simple Example, you need to be sure that you close and dispose your Mutex or else you see more problems when you update your page. For example if the Mutex stay on memory locked wile you update your page, then your page may lock for long time, until the Garbage collection kill it, if they do.

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

Sidebar

Related Questions

Could you describe two methods of synchronizing multi-threaded write access performed on a class
I have a variable declared as: private static List<String> _search_result_list = new ArrayList<String>( 15
I'm using ASP.NET's in-process session state store. It locks access to a session exclusively,
I'm synchronizing reader and writer processes on Linux. I have 0 or more process
What is the best approach to synchronizing a DataSet with data in a database?
Is there an elegant solution for synchronizing sessions across multiple ASP.NET applications? I'm sure
I'm interested in T-SQL source code for synchronizing a table (or perhaps a subset
I need to access a file larger than 2gb using C. During one run
I've got a client who has a very large internal system using Access, which
We use a back-office accounting system with an SQL db2 database. Our sales, service

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.