예제 #1
0
        private void ExecuteStatementsInternal(
            IEnumerable <MigrationStatement> migrationStatements,
            DbConnection connection)
        {
            DbContext             context             = this._usersContext ?? this._usersContextInfo.CreateInstance();
            DbInterceptionContext interceptionContext = new DbInterceptionContext().WithDbContext(context);
            TransactionHandler    transactionHandler  = (TransactionHandler)null;

            try
            {
                if (DbInterception.Dispatch.Connection.GetState(connection, interceptionContext) == ConnectionState.Broken)
                {
                    DbInterception.Dispatch.Connection.Close(connection, interceptionContext);
                }
                if (DbInterception.Dispatch.Connection.GetState(connection, interceptionContext) == ConnectionState.Closed)
                {
                    DbInterception.Dispatch.Connection.Open(connection, interceptionContext);
                }
                if (!(context is TransactionContext))
                {
                    Func <TransactionHandler> service = DbConfiguration.DependencyResolver.GetService <Func <TransactionHandler> >((object)new ExecutionStrategyKey(DbConfiguration.DependencyResolver.GetService <IProviderInvariantName>((object)DbProviderServices.GetProviderFactory(connection)).Name, DbInterception.Dispatch.Connection.GetDataSource(connection, interceptionContext)));
                    if (service != null)
                    {
                        transactionHandler = service();
                        transactionHandler.Initialize(context, connection);
                    }
                }
                this.ExecuteStatementsInternal(migrationStatements, connection, interceptionContext);
                this._committedStatements = true;
            }
            finally
            {
                transactionHandler?.Dispose();
                if (this._usersContext == null)
                {
                    context.Dispose();
                }
            }
        }
예제 #2
0
        private void ExecuteStatementsInternal(IEnumerable <MigrationStatement> migrationStatements, DbConnection connection)
        {
            DebugCheck.NotNull(migrationStatements);
            DebugCheck.NotNull(connection);

            var context = _contextForInterception ?? _usersContextInfo.CreateInstance();

            var interceptionContext = new DbInterceptionContext();

            interceptionContext = interceptionContext.WithDbContext(context);

            DbInterception.Dispatch.Connection.Open(connection, interceptionContext);

            TransactionHandler transactionHandler = null;

            try
            {
                if (!(context is TransactionContext))
                {
                    var providerInvariantName =
                        DbConfiguration.DependencyResolver.GetService <IProviderInvariantName>(
                            DbProviderServices.GetProviderFactory(connection))
                        .Name;

                    var dataSource = DbInterception.Dispatch.Connection.GetDataSource(connection, interceptionContext);

                    var transactionHandlerFactory = DbConfiguration.DependencyResolver.GetService <Func <TransactionHandler> >(
                        new StoreKey(providerInvariantName, dataSource));

                    if (transactionHandlerFactory != null)
                    {
                        transactionHandler = transactionHandlerFactory();
                        transactionHandler.Initialize(context, connection);
                    }
                }

                var beginTransactionInterceptionContext = new BeginTransactionInterceptionContext(interceptionContext)
                                                          .WithIsolationLevel(IsolationLevel.Serializable);

                using (var transaction = DbInterception.Dispatch.Connection.BeginTransaction(
                           connection,
                           beginTransactionInterceptionContext))
                {
                    foreach (var migrationStatement in migrationStatements)
                    {
                        base.ExecuteSql(transaction, migrationStatement, interceptionContext);
                    }

                    DbInterception.Dispatch.Transaction.Commit(transaction, interceptionContext);
                }
            }
            finally
            {
                if (transactionHandler != null)
                {
                    transactionHandler.Dispose();
                }

                if (_contextForInterception == null)
                {
                    context.Dispose();
                }
            }
        }