Exemplo n.º 1
0
        protected virtual async Task SaveChangesCoreAsync(SaveWorkState saveWorkState)
        {
            var saveMap   = saveWorkState.SaveMap;
            var flushMode = Session.FlushMode;

            Session.FlushMode = FlushMode.Manual;
            var tx = Session.Transaction;
            var hasExistingTransaction = tx.IsActive;

            if (!hasExistingTransaction)
            {
                tx.Begin(_breezeConfig.GetTransactionSettings().IsolationLevelAs);
            }

            try
            {
                // Relate entities in the saveMap to other NH entities, so NH can save the FK values.
                var fixer     = GetRelationshipFixer(saveMap);
                var saveOrder = fixer.FixupRelationships();
                // Allow subclass to process entities before we save them
                AddKeyMappings(saveOrder);
                saveOrder = await BeforeSaveEntityGraphAsync(saveOrder);
                await ProcessSavesAsync(saveOrder);
                await BeforeFlushAsync(saveOrder);

                await Session.FlushAsync();

                Session.FlushMode = flushMode;
                await AfterFlushAsync(saveOrder);
                await RefreshFromSessionAsync(saveMap);

                if (!hasExistingTransaction)
                {
                    await tx.CommitAsync();
                }
                fixer.RemoveRelationships();
            }
            catch (PropertyValueException pve)
            {
                // NHibernate can throw this
                if (!hasExistingTransaction)
                {
                    tx.Rollback();
                }
                _entityErrors.Add(new EntityError
                {
                    EntityTypeName = pve.EntityName,
                    ErrorMessage   = pve.Message,
                    ErrorName      = "PropertyValueException",
                    KeyValues      = null,
                    PropertyName   = pve.PropertyName
                });
                saveWorkState.EntityErrors = _entityErrors;
            }
            catch (Exception)
            {
                if (!hasExistingTransaction)
                {
                    tx.Rollback();
                }
                throw;
            }
            finally
            {
                Session.FlushMode = flushMode;
                if (!hasExistingTransaction)
                {
                    tx.Dispose();
                }
            }

            saveWorkState.KeyMappings = UpdateAutoGeneratedKeys(saveWorkState.EntitiesWithAutoGeneratedKeys);
        }
Exemplo n.º 2
0
        public async Task <SaveResult> SaveChangesAsync(JObject saveBundle,
                                                        TransactionSettings transactionSettings = null)
        {
            if (SaveWorkState == null || SaveWorkState.WasUsed)
            {
                InitializeSaveState(saveBundle);
            }

            transactionSettings = transactionSettings ?? _breezeConfig.GetTransactionSettings();
            try
            {
                if (transactionSettings.TransactionType == TransactionType.TransactionScope)
                {
                    var txOptions = transactionSettings.ToTransactionOptions();
                    using (var txScope = new TransactionScope(TransactionScopeOption.Required, txOptions))
                    {
                        await OpenAndSaveAsync(SaveWorkState);

                        txScope.Complete();
                    }
                }
                else if (transactionSettings.TransactionType == TransactionType.DbTransaction)
                {
                    OpenDbConnection();
                    using (var tran = BeginTransaction(transactionSettings.IsolationLevelAs))
                    {
                        try
                        {
                            await OpenAndSaveAsync(SaveWorkState);

                            await Session.Transaction.CommitAsync();
                        }
                        catch
                        {
                            Session.Transaction.Rollback();
                            throw;
                        }
                    }
                }
                else
                {
                    await OpenAndSaveAsync(SaveWorkState);
                }
            }
            catch (EntityErrorsException e)
            {
                SaveWorkState.EntityErrors = e.EntityErrors;
                throw;
            }
            catch (Exception e2)
            {
                if (!HandleSaveException(e2, SaveWorkState))
                {
                    throw;
                }
            }
            finally
            {
                CloseDbConnection();
            }

            return(SaveWorkState.ToSaveResult());
        }
Exemplo n.º 3
0
        protected override bool HandleSaveException(Exception e, SaveWorkState saveWorkState)
        {
            Session.RollbackTransaction();

            return(base.HandleSaveException(e, saveWorkState));
        }