static OutboxPersister CreateOutboxPersister(IConnectionManager connectionManager, SqlDialect sqlDialect, bool pessimisticMode, bool transactionScopeMode)
        {
            var outboxCommands = OutboxCommandBuilder.Build(sqlDialect, "PersistenceTests_");
            ConcurrencyControlStrategy concurrencyControlStrategy;

            if (pessimisticMode)
            {
                concurrencyControlStrategy = new PessimisticConcurrencyControlStrategy(sqlDialect, outboxCommands);
            }
            else
            {
                concurrencyControlStrategy = new OptimisticConcurrencyControlStrategy(sqlDialect, outboxCommands);
            }

            ISqlOutboxTransaction transactionFactory()
            {
                return(transactionScopeMode
                    ? (ISqlOutboxTransaction) new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, IsolationLevel.ReadCommitted)
                    : new AdoNetSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, System.Data.IsolationLevel.ReadCommitted));
            }

            var outboxPersister = new OutboxPersister(connectionManager, sqlDialect, outboxCommands, transactionFactory);

            return(outboxPersister);
        }
Beispiel #2
0
    OutboxPersister Setup(string theSchema)
    {
        var dialect        = sqlDialect.Convert(theSchema);
        var outboxCommands = OutboxCommandBuilder.Build(dialect, $"{GetTablePrefix()}_");

        var connectionManager = new ConnectionManager(() => GetConnection()(theSchema));
        var persister         = new OutboxPersister(
            connectionManager: connectionManager,
            sqlDialect: dialect,
            outboxCommands: outboxCommands,
            outboxTransactionFactory: () =>
        {
            ConcurrencyControlStrategy behavior;
            if (pessimistic)
            {
                behavior = new PessimisticConcurrencyControlStrategy(dialect, outboxCommands);
            }
            else
            {
                behavior = new OptimisticConcurrencyControlStrategy(dialect, outboxCommands);
            }

            return(transactionScope
                    ? (ISqlOutboxTransaction) new TransactionScopeSqlOutboxTransaction(behavior, connectionManager, IsolationLevel.ReadCommitted)
                    : new AdoNetSqlOutboxTransaction(behavior, connectionManager, System.Data.IsolationLevel.ReadCommitted));
        },
            cleanupBatchSize: 5);

        using (var connection = GetConnection()(theSchema))
        {
            connection.Open();
            connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(sqlDialect), GetTablePrefix(), schema: theSchema);
            connection.ExecuteCommand(OutboxScriptBuilder.BuildCreateScript(sqlDialect), GetTablePrefix(), schema: theSchema);
        }
        return(persister);
    }
Beispiel #3
0
    protected override void Setup(FeatureConfigurationContext context)
    {
        var settings          = context.Settings;
        var connectionManager = settings.GetConnectionBuilder <StorageType.Outbox>();
        var tablePrefix       = settings.GetTablePrefix();
        var sqlDialect        = settings.GetSqlDialect();

        var pessimisticMode      = context.Settings.GetOrDefault <bool>(ConcurrencyMode);
        var transactionScopeMode = context.Settings.GetOrDefault <bool>(TransactionMode);

        var outboxCommands = OutboxCommandBuilder.Build(sqlDialect, tablePrefix);

        ConcurrencyControlStrategy concurrencyControlStrategy;

        if (pessimisticMode)
        {
            concurrencyControlStrategy = new PessimisticConcurrencyControlStrategy(sqlDialect, outboxCommands);
        }
        else
        {
            concurrencyControlStrategy = new OptimisticConcurrencyControlStrategy(sqlDialect, outboxCommands);
        }

        ISqlOutboxTransaction transactionFactory()
        {
            return(transactionScopeMode
                ? (ISqlOutboxTransaction) new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager)
                : new AdoNetSqlOutboxTransaction(concurrencyControlStrategy, connectionManager));
        }

        var outboxPersister = new OutboxPersister(connectionManager, sqlDialect, outboxCommands, transactionFactory);

        context.Container.ConfigureComponent(b => outboxPersister, DependencyLifecycle.InstancePerCall);

        if (settings.GetOrDefault <bool>(DisableCleanup))
        {
            settings.AddStartupDiagnosticsSection("NServiceBus.Persistence.Sql.Outbox", new
            {
                CleanupDisabled = true
            });

            return;
        }

        if (settings.EndpointIsMultiTenant())
        {
            throw new Exception($"{nameof(SqlPersistenceConfig.MultiTenantConnectionBuilder)} can only be used with the Outbox feature if Outbox cleanup is handled by an external process (i.e. SQL Agent) and the endpoint is configured to disable Outbox cleanup using endpointConfiguration.EnableOutbox().{nameof(SqlPersistenceOutboxSettingsExtensions.DisableCleanup)}(). See the SQL Persistence documentation for more information on how to clean up Outbox tables from a scheduled task.");
        }

        var frequencyToRunCleanup       = settings.GetOrDefault <TimeSpan?>(FrequencyToRunDeduplicationDataCleanup) ?? TimeSpan.FromMinutes(1);
        var timeToKeepDeduplicationData = settings.GetOrDefault <TimeSpan?>(TimeToKeepDeduplicationData) ?? TimeSpan.FromDays(7);

        settings.AddStartupDiagnosticsSection("NServiceBus.Persistence.Sql.Outbox", new
        {
            CleanupDisabled                        = false,
            TimeToKeepDeduplicationData            = timeToKeepDeduplicationData,
            FrequencyToRunDeduplicationDataCleanup = frequencyToRunCleanup
        });

        context.RegisterStartupTask(b =>
                                    new OutboxCleaner(outboxPersister.RemoveEntriesOlderThan, b.Build <CriticalError>().Raise, timeToKeepDeduplicationData, frequencyToRunCleanup, new AsyncTimer()));
    }