protected override void ExecuteImpl(Stopwatch netTimeStopwatch)
    {
        var parameters   = new Dictionary <string, object>();
        var sqlStatement = CreateSqlStatement(parameters);

        using (var scope = SuppressExistingTransactionScope ? new TransactionScope(TransactionScopeOption.Suppress) : null)
        {
            var connection = EtlConnectionManager.GetConnection(ConnectionString, this);
            try
            {
                lock (connection.Lock)
                {
                    using (var cmd = connection.Connection.CreateCommand())
                    {
                        cmd.CommandTimeout = CommandTimeout;
                        cmd.CommandText    = sqlStatement;
                        cmd.FillCommandParameters(parameters);

                        var transactionId = Transaction.Current.ToIdentifierString();
                        RunCommand(cmd, transactionId, parameters);
                    }
                }
            }
            finally
            {
                EtlConnectionManager.ReleaseConnection(this, ref connection);
            }
        }
    }
Example #2
0
    protected override void ExecuteImpl(Stopwatch netTimeStopwatch)
    {
        using (var scope = SuppressExistingTransactionScope ? new TransactionScope(TransactionScopeOption.Suppress) : null)
        {
            var connection = EtlConnectionManager.GetConnection(ConnectionString, this);
            try
            {
                lock (connection.Lock)
                {
                    var transactionId = Transaction.Current.ToIdentifierString();

                    // todo: support returning parameters
                    var sqlStatements = CreateSqlStatements(ConnectionString, connection.Connection, transactionId);
                    if (sqlStatements.Count > 0)
                    {
                        using (var cmd = connection.Connection.CreateCommand())
                        {
                            cmd.CommandTimeout = CommandTimeout;

                            var startedOn = Stopwatch.StartNew();

                            for (var i = 0; i < sqlStatements.Count; i++)
                            {
                                var sqlStatement = sqlStatements[i];

                                cmd.CommandText = sqlStatement;
                                try
                                {
                                    startedOn.Restart();
                                    RunCommand(cmd, i, startedOn, transactionId);
                                }
                                catch (Exception)
                                {
                                    LogSucceeded(i - 1, transactionId);
                                    throw;
                                }
                            }

                            LogSucceeded(sqlStatements.Count - 1, transactionId);
                        }
                    }
                }
            }
            finally
            {
                EtlConnectionManager.ReleaseConnection(this, ref connection);
            }
        }
    }
Example #3
0
        public void ValidateConnectionStrings(
            [Operand(Name = "names", Description = "The space-separated list of module names.")] List <string> moduleNames,
            [Option(LongName = "all", ShortName = "a")] bool all)
        {
            if (moduleNames == null || moduleNames.Count == 0)
            {
                if (!all)
                {
                    CommandLineHandler.DisplayHelp("test connection-strings");
                    return;
                }
            }
            else if (all)
            {
                CommandLineHandler.DisplayHelp("test connection-strings");
                return;
            }

            var commandContext = CommandLineHandler.Context;

            if (all)
            {
                moduleNames = ModuleLister.GetAllModules(commandContext);
            }

            var allConnectionStrings = new List <NamedConnectionString>();
            var index = 0;

            foreach (var moduleName in moduleNames)
            {
                var moduleConfiguration = ModuleConfigurationLoader.LoadModuleConfiguration(commandContext, moduleName, null, null);
                if (moduleConfiguration == null)
                {
                    continue;
                }

                if (index == 0)
                {
                    var sharedCs = new ConnectionStringCollection();
                    sharedCs.LoadFromConfiguration(moduleConfiguration.Configuration, "ConnectionStrings:Shared", commandContext.HostConfiguration.SecretProtector);
                    if (sharedCs.All.Any())
                    {
                        commandContext.Logger.Information("connection strings for: {Module}", "Shared");
                        foreach (var cs in sharedCs.All)
                        {
                            commandContext.Logger.Information("\t{ConnectionStringName} ({Provider})", cs.Name, cs.GetFriendlyProviderName());
                            allConnectionStrings.Add(cs);
                        }
                    }
                }

                commandContext.Logger.Information("connection strings for: {Module}", moduleName);

                var connectionStrings = new ConnectionStringCollection();
                connectionStrings.LoadFromConfiguration(moduleConfiguration.Configuration, "ConnectionStrings:Module", commandContext.HostConfiguration.SecretProtector);
                foreach (var cs in connectionStrings.All)
                {
                    commandContext.Logger.Information("\t{ConnectionStringName} ({Provider})", cs.Name, cs.GetFriendlyProviderName());
                    allConnectionStrings.RemoveAll(x => x.Name == cs.Name);
                    allConnectionStrings.Add(cs);
                }

                index++;
            }

            commandContext.Logger.Information("relevant connection strings");
            var originalNames = allConnectionStrings
                                .Select(x => x.Name.Split('-')[0])
                                .Distinct()
                                .ToList();

            foreach (var originalName in originalNames)
            {
                var connectionString = allConnectionStrings.Find(x => string.Equals(x.Name, originalName + "-" + Environment.MachineName, StringComparison.InvariantCultureIgnoreCase))
                                       ?? allConnectionStrings.Find(x => string.Equals(x.Name, originalName, StringComparison.InvariantCultureIgnoreCase));

                var knownFields = connectionString.GetKnownConnectionStringFields();
                if (knownFields == null)
                {
                    commandContext.Logger.Information("\ttesting: {ConnectionStringName} ({Provider})",
                                                      connectionString.Name, connectionString.GetFriendlyProviderName());
                }
                else
                {
                    var message = "\ttesting: {ConnectionStringName} ({Provider})";
                    var args    = new List <object>()
                    {
                        connectionString.Name,
                        connectionString.GetFriendlyProviderName(),
                    };

                    if (knownFields.Server != null)
                    {
                        message += ", server: {Server}";
                        args.Add(knownFields.Server);
                    }

                    if (knownFields.Port != null)
                    {
                        message += ", port: {Port}";
                        args.Add(knownFields.Port);
                    }

                    if (knownFields.Database != null)
                    {
                        message += ", database: {Database}";
                        args.Add(knownFields.Database);
                    }

                    if (knownFields.IntegratedSecurity != null)
                    {
                        message += ", integrated security: {IntegratedSecurity}";
                        args.Add(knownFields.IntegratedSecurity);
                    }

                    if (knownFields.UserId != null)
                    {
                        message += ", user: {UserId}";
                        args.Add(knownFields.UserId);
                    }

                    commandContext.Logger.Information(message, args.ToArray());
                }

                try
                {
                    EtlConnectionManager.TestConnection(connectionString);
                    commandContext.Logger.Information("\t\tPASSED");
                }
                catch (Exception ex)
                {
                    commandContext.Logger.Write(LogEventLevel.Fatal, "\t\t{ErrorMessage}", ex.FormatExceptionWithDetails(false));
                }
            }

            commandContext.Logger.Information("connection string test(s) finished");
        }