/// <summary> /// Commit a transaction. /// </summary> /// <param name="connectionFactory"></param> /// <param name="onCompleted"></param> public void Commit(IConnectionFactory connectionFactory, Action onCompleted) { // update the counter. using (_counterCommit.Start()) { // we don't need the lock for readonly if (connectionFactory.IsReadOnly) { connectionFactory.Commit(); onCompleted(); return; } var lockTaken = false; try { Monitor.Enter(_lock, ref lockTaken); // if this is not a readonly factory // and this is not our factory, then we do not know where it comes from. if (connectionFactory != _writeFactory) { throw new ArgumentException("The given transaction was not created by this class"); } CommitInLock(onCompleted); } finally { if (lockTaken) { Monitor.Exit(_lock); } } } }
/// <summary> /// Commit the tansaction while we are in lock. /// </summary> /// <param name="onCompleted"></param> private void CommitInLock(Action onCompleted) { try { // try and commit _writeFactory?.Commit(); onCompleted(); } finally { // whatever happens, we are done with the transaction // it is up to the factory to catch/handle any issues. _writeFactory = null; } }