This is a migrated thread and some comments may be shown as answers.

Is static iObjectScope Threadsafe

1 Answer 62 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ben Bolton
Top achievements
Rank 1
Ben Bolton asked on 23 Feb 2010, 06:23 PM

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;

 

 }

1 Answer, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 25 Feb 2010, 08:40 AM
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
Tags
General Discussions
Asked by
Ben Bolton
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Share this question
or