Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

UnitOfWorkManager is not Thread Safe #16

Open
duereg opened this issue Aug 23, 2010 · 1 comment
Open

UnitOfWorkManager is not Thread Safe #16

duereg opened this issue Aug 23, 2010 · 1 comment

Comments

@duereg
Copy link

duereg commented Aug 23, 2010

UnitOfWorkManager is not thread safe. Multiple TransactionManagers can be created in a multi-threaded environment.

To fix this issue, I've changed the following method to add a double-lock on the TransactionManager creation.

private static readonly object _lock = new Object();
static readonly Func DefaultTransactionManager = () =>
{
_logger.Debug(x => x("Using default UnitOfWorkManager provider to resolve current transaction manager."));
var state = ServiceLocator.Current.GetInstance();
var transactionManager = state.Local.Get(LocalTransactionManagerKey);
if (transactionManager == null)
{
lock (_lock)
{
if (transactionManager == null)
{
_logger.Debug(x => x("No valid ITransactionManager found in Local state. Creating a new TransactionManager."));
transactionManager = new TransactionManager();
state.Local.Put(LocalTransactionManagerKey, transactionManager);
}
}
}
return transactionManager;
};

@jimitndiaye
Copy link

The second check for transactionManager will always return true since you're just testing the local variable. You should refresh the transactionManager from the local state again:

                      lock (_lock)
                      {
                          if ((transactionManager = state.Local.Get<ITransactionManager>(LocalTransactionManagerKey))== null)
                          { ..}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants