protected virtual void PrepareSynchronization(DefaultTransactionStatus status, ITransactionDefinition definition)
 {
     if (status.IsNewSynchronization)
     {
         TransactionSynchronizationManager.SetActualTransactionActive(status.HasTransaction);
         TransactionSynchronizationManager.SetCurrentTransactionIsolationLevel(definition.IsolationLevel != AbstractTransactionDefinition.ISOLATION_DEFAULT ? definition.IsolationLevel : (int?)null);
         TransactionSynchronizationManager.SetCurrentTransactionReadOnly(definition.IsReadOnly);
         TransactionSynchronizationManager.SetCurrentTransactionName(definition.Name);
         TransactionSynchronizationManager.InitSynchronization();
     }
 }
        protected virtual SuspendedResourcesHolder Suspend(object transaction)
        {
            if (TransactionSynchronizationManager.IsSynchronizationActive())
            {
                var suspendedSynchronizations = DoSuspendSynchronization();
                try
                {
                    object suspendedResources = null;
                    if (transaction != null)
                    {
                        suspendedResources = DoSuspend(transaction);
                    }

                    var name = TransactionSynchronizationManager.GetCurrentTransactionName();
                    TransactionSynchronizationManager.SetCurrentTransactionName(null);
                    var readOnly = TransactionSynchronizationManager.IsCurrentTransactionReadOnly();
                    TransactionSynchronizationManager.SetCurrentTransactionReadOnly(false);
                    var isolationLevel = TransactionSynchronizationManager.GetCurrentTransactionIsolationLevel();
                    TransactionSynchronizationManager.SetCurrentTransactionIsolationLevel(null);
                    var wasActive = TransactionSynchronizationManager.IsActualTransactionActive();
                    TransactionSynchronizationManager.SetActualTransactionActive(false);
                    return(new SuspendedResourcesHolder(suspendedResources, suspendedSynchronizations, name, readOnly, isolationLevel, wasActive));
                }
                catch (Exception)
                {
                    // doSuspend failed - original transaction is still active...
                    DoResumeSynchronization(suspendedSynchronizations);
                    throw;
                }
            }
            else if (transaction != null)
            {
                // Transaction active but no synchronization active.
                var suspendedResources = DoSuspend(transaction);
                return(new SuspendedResourcesHolder(suspendedResources));
            }
            else
            {
                // Neither transaction nor synchronization active.
                return(null);
            }
        }
        protected virtual void Resume(object transaction, SuspendedResourcesHolder resourcesHolder)
        {
            if (resourcesHolder != null)
            {
                var suspendedResources = resourcesHolder.SuspendedResources;
                if (suspendedResources != null)
                {
                    DoResume(transaction, suspendedResources);
                }

                var suspendedSynchronizations = resourcesHolder.SuspendedSynchronizations;
                if (suspendedSynchronizations != null)
                {
                    TransactionSynchronizationManager.SetActualTransactionActive(resourcesHolder.WasActive);
                    TransactionSynchronizationManager.SetCurrentTransactionIsolationLevel(resourcesHolder.IsolationLevel);
                    TransactionSynchronizationManager.SetCurrentTransactionReadOnly(resourcesHolder.ReadOnly);
                    TransactionSynchronizationManager.SetCurrentTransactionName(resourcesHolder.Name);
                    DoResumeSynchronization(suspendedSynchronizations);
                }
            }
        }