Is static iObjectScope Threadsafe

Thread is closed for posting
2 posts, 0 answers
  1. Ben Bolton
    Ben Bolton avatar
    5 posts
    Member since:
    Aug 2009

    Posted 23 Feb 2010 Link to this post

    I've read that the ORM object scope object was designed to support multi threading.  We have a static class with the following definition:

     

     

    internal static IObjectScope _os = XBDLPScopeProvider.GetNewObjectScope();

     

     

    public static IObjectScope ORMScope

     

    {

     

    get { return _os; }

     

    }

    • Can methods running in multiple threads safely update persisted objects using the ORMScope (see code below) 

     

    • If it is not safe what is the recommended mechanism for doing multiple threadsafe updates.

    Ben

     

    public static void SetRequestStatus(Guid requestID, int recordsRead, int recordsAnalyzed)

     

    {

     

    var request = (IStatusParent) GetRunningRequest(requestID);

     

     

    try

     

     {

     

        // Insert to database

     

     

     

        DLPDB.ORMScope.Transaction.Begin();

     

        {

            request.Status.RecordsRead = recordsRead;

            request.Status.RecordsAnalyzed = recordsAnalyzed;

        }

        DLPDB.ORMScope.Transaction.Commit();

     }

    catch (Exception ex)

     

     {

     

       DLPDB.ORMScope..Transaction.Rollback();

     

     

      throw;

     

     }

  2. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 25 Feb 2010 Link to this post

    Hi Ben,

    It really depends on your application I guess..

    We have a set of WCF services hosted on IIS... Naturally several threads (requests) can access the persistent model at the same time.
    So, in this scenario (which I believe is a typical multithread scenario), you can use the pattern of "one thread - one object scope", meaning that each thread has it's own object scope.. And you have a mechanism to "bind" the object scope to the thread, so that it lives and dies with the thread. Each time you need access to this scope from within a given thread the mechanism makes sure that you get the object scope bound to the thread requesting it.

    Regards

    Henrik
Back to Top