public virtual async Task <DbConnection> GetDbConnectionAsync(string connectionString, bool rollbackOnScopeStatusFailure,
                                                                      CancellationToken cancellationToken)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            if (!DbConnectionAndTransactions.Any(dbAndTran => dbAndTran.ConnectionString == connectionString && dbAndTran.RollbackOnScopeStatusFailure == rollbackOnScopeStatusFailure))
            {
                TDbConnection newConnection = new TDbConnection {
                    ConnectionString = connectionString
                };
                DbTransaction transaction = null;
                try
                {
                    await newConnection.OpenAsync(cancellationToken).ConfigureAwait(false);

                    transaction = newConnection.BeginTransaction(IsolationLevel);
                }
                catch { }
                DbConnectionAndTransactions.Add(new DbConnectionAndTransactionPair(connectionString, newConnection, transaction, rollbackOnScopeStatusFailure));
            }

            return(DbConnectionAndTransactions.ExtendedSingle($"Getting db connection for {connectionString}", dbAndTran => dbAndTran.ConnectionString == connectionString && dbAndTran.RollbackOnScopeStatusFailure == rollbackOnScopeStatusFailure).Connection);
        }
Exemplo n.º 2
0
        public virtual DbConnection GetDbConnection(string connectionString, bool rollbackOnScopeStatusFailure)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            if (!DbConnectionAndTransactions.Any(dbAndTran => dbAndTran.ConnectionString == connectionString && dbAndTran.RollbackOnScopeStatusFailure == rollbackOnScopeStatusFailure))
            {
                TDbConnection newConnection = new TDbConnection {
                    ConnectionString = connectionString
                };
                DbTransaction?transaction = null;
                try
                {
                    newConnection.Open();
                    transaction = newConnection.BeginTransaction(IsolationLevel);
                }
                catch { }
                DbConnectionAndTransactions.Add(new DbConnectionAndTransactionPair(connectionString, newConnection, transaction, rollbackOnScopeStatusFailure));
            }

            return(DbConnectionAndTransactions.ExtendedSingle($"Getting db connection for {connectionString}", dbAndTran => dbAndTran.ConnectionString == connectionString && dbAndTran.RollbackOnScopeStatusFailure == rollbackOnScopeStatusFailure).Connection);
        }