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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:29:28+00:00 2026-06-08T15:29:28+00:00

I read many posts saying multithreaded applications must use a separate session per thread.

  • 0

I read many posts saying multithreaded applications must use a separate session per thread. Perhaps I don’t understand how the locking works, but if I put a lock on the session in all repository methods, would that not make a single static session thread safe?

like:

public void SaveOrUpdate(T instance)
{
    if (instance == null) return;
    lock (_session)
        using (ITransaction transaction = _session.BeginTransaction())
        {
            lock (instance)
            {
                _session.SaveOrUpdate(instance);
                transaction.Commit();
            }
        }
}

EDIT:

Please consider the context/type of applications I’m writing:

Not multi-user, not typical user-interaction, but a self-running robot reacting to remote events like financial data and order-updates, performing tasks and saves based on that. Intermittently this can create clusters of up to 10 saves per second. Typically it’s the same object graph that needs to be saved every time. Also, on startup, the program does load the full database into an entity-object-graph. So it basically just reads once, then performs SaveOrUpdates as it runs.

  • 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-06-08T15:29:30+00:00Added an answer on June 8, 2026 at 3:29 pm

    Given that the application is typically editing the same object graph, perhaps it would make more sense to have a single thread dedicated to applying these edits to the object graph and then saving them to the database, or perhaps a pool of threads servicing a common queue of edits, where each thread has it’s own (dedicated) session that it does not need to lock. Look up producer/consumer queues (to start, look here).

    Something like this:

    [Producer Threads]
    Edit Event -\                [Database Servicer Thread]
    Edit Event ------> Queue -> Dequeue and Apply to Session -> Database
    Edit Event -/ 
    

    I’d imagine that a BlockingCollection<Action<Session>> would be a good starting point for such an implementation.

    Here’s a rough example (note this is obviously untested):

    // Assuming you have a work queue defined as 
    public static BlockingCollection<Action<Session>> myWorkQueue = new BlockingCollection<Action<Session>>();
    
    // and your eventargs looks something like this
    public class MyObjectUpdatedEventArgs : EventArgs {
        public MyObject MyObject { get; set; }
    }
    
    // And one of your event handlers
    public MyObjectWasChangedEventHandler(object sender, MyObjectUpdatedEventArgs e) {
        myWorkQueue.Add(s=>SaveOrUpdate(e.MyObject));
    }
    
    // Then a thread in a constant loop processing these items could work:
    public void ProcessWorkQueue() {
        var mySession = mySessionFactory.CreateSession();
        while (true) {
            var nextWork = myWorkQueue.Take();
            nextWork(mySession);
        }
    }
    
    // And to run the above:
    var dbUpdateThread = new Thread(ProcessWorkQueue);
    dbUpdateThread.IsBackground = true;
    dbUpdateThread.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read many posts about this, the last being a statement saying the behaviour
I have read many posts about this now but I do not still understand
I have read many posts on Session-scoped data in MVC, but I am still
I've read so many posts and still can't find or understand how to handle
I've read many posts about reloading the Activity. I understand it is not the
I've read many thread posts, articles etc. about binding and the thread affinity of
I've read many posts about doing Rails nested forms, which I think I understand.
I read many posts about this problem like [this link][1] and one solution is
I've read MANY different solutions for the separate functions of LINQ that, when put
I've read many posts about this issue but I never got this to work.

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.