Ejemplo n.º 1
0
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction current = Transaction.Current;

            if (current == (Transaction)null)
            {
                return((DatabaseConnectionWrapper)null);
            }
            Dictionary <string, DatabaseConnectionWrapper> dictionary;

            lock (TransactionScopeConnections.transactionConnections)
            {
                if (!TransactionScopeConnections.transactionConnections.TryGetValue(current, out dictionary))
                {
                    dictionary = new Dictionary <string, DatabaseConnectionWrapper>();
                    TransactionScopeConnections.transactionConnections.Add(current, dictionary);
                    current.TransactionCompleted += new TransactionCompletedEventHandler(TransactionScopeConnections.OnTransactionCompleted);
                }
            }
            DatabaseConnectionWrapper connectionWrapper;

            lock (dictionary)
            {
                if (!dictionary.TryGetValue(db.ConnectionString, out connectionWrapper))
                {
                    connectionWrapper = new DatabaseConnectionWrapper(db.GetNewOpenConnection());
                    dictionary.Add(db.ConnectionString, connectionWrapper);
                }
                connectionWrapper.AddRef();
            }
            return(connectionWrapper);
        }
 public RefCountingDataReaderForAsync(DatabaseConnectionWrapper connection, DbDataReader innerReader)
     : base(innerReader)
 {
     Guard.ArgumentNotNull((object)connection, nameof(connection));
     Guard.ArgumentNotNull((object)innerReader, nameof(innerReader));
     this.connectionWrapper = connection;
     this.connectionWrapper.AddRef();
 }
Ejemplo n.º 3
0
 public virtual async Task <int> ExecuteNonQueryAsync(DbCommand command)
 {
     using (DatabaseConnectionWrapper openConnection = await this.GetOpenConnectionAsync())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         return(await this.DoExecuteNonQueryAsync(command));
     }
 }
Ejemplo n.º 4
0
 public virtual int ExecuteNonQuery(DbCommand command)
 {
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         return(this.DoExecuteNonQuery(command));
     }
 }
Ejemplo n.º 5
0
 public virtual void LoadDataSet(DbCommand command, DataSet dataSet, string[] tableNames)
 {
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         this.DoLoadDataSet((IDbCommand)command, dataSet, tableNames);
     }
 }
        /// <summary>
        /// Create a new <see cref='RefCountingDataReader'/> that wraps
        /// the given <paramref name="innerReader"/> and properly
        /// cleans the refcount on the given <paramref name="connection"/>
        /// when done.
        /// </summary>
        /// <param name="connection">Connection to close.</param>
        /// <param name="innerReader">Reader to do the actual work.</param>
        public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
            : base(innerReader)
        {
            Guard.ArgumentNotNull(connection, "connection");
            Guard.ArgumentNotNull(innerReader, "innerReader");

            connectionWrapper = connection;
            connectionWrapper.AddRef();
        }
        /// <summary>
        /// Create a new <see cref='RefCountingDataReader'/> that wraps
        /// the given <paramref name="innerReader"/> and properly
        /// cleans the refcount on the given <paramref name="connection"/>
        /// when done.
        /// </summary>
        /// <param name="connection">Connection to close.</param>
        /// <param name="innerReader">Reader to do the actual work.</param>
        public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
            : base(innerReader)
        {
            Guard.ArgumentNotNull(connection, "connection");
            Guard.ArgumentNotNull(innerReader, "innerReader");

            connectionWrapper = connection;
            connectionWrapper.AddRef();
        }
Ejemplo n.º 8
0
 public virtual IDataReader ExecuteReader(DbCommand command)
 {
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         IDataReader innerReader = this.DoExecuteReader(command, CommandBehavior.Default);
         return(this.CreateWrappedReader(openConnection, innerReader));
     }
 }
Ejemplo n.º 9
0
        public virtual async Task <DbDataReader> ExecuteReaderAsync(DbCommand command)
        {
            using (DatabaseConnectionWrapper openConnection = await this.GetOpenConnectionAsync())
            {
                Database.PrepareCommand(command, openConnection.Connection);
                DbDataReader innerReader = await this.DoExecuteReaderAsync(command, CommandBehavior.CloseConnection);

                return(this.CreateWrappedReaderForAsync(openConnection, innerReader));
            }
        }
Ejemplo n.º 10
0
 public virtual async Task <object> ExecuteScalarAsync(DbCommand command)
 {
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     using (DatabaseConnectionWrapper openConnection = await this.GetOpenConnectionAsync())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         return(await this.DoExecuteScalarAsync(command));
     }
 }
Ejemplo n.º 11
0
 public virtual object ExecuteScalar(DbCommand command)
 {
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         Database.PrepareCommand(command, openConnection.Connection);
         return(this.DoExecuteScalar((IDbCommand)command));
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        ///        Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
        ///        instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
        ///        returns null.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
            {
                return(null);
            }

            Dictionary <string, DatabaseConnectionWrapper> connectionList;
            DatabaseConnectionWrapper connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    // We don't have a list for this transaction, so create a new one
                    connectionList = new Dictionary <string, DatabaseConnectionWrapper>();
                    transactionConnections.Add(currentTransaction, connectionList);

                    // We need to know when this previously unknown transaction is completed too
                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }

            lock (connectionList)
            {
                // Next we'll see if there is already a connection. If not, we'll create a new connection and add it
                // to the transaction's list of connections.
                // This collection should only be modified by the thread where the transaction scope was created
                // while the transaction scope is active.
                // However there's no documentation to confirm this, so we err on the safe side and lock.
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    // we're betting the cost of acquiring a new finer-grained lock is less than
                    // that of opening a new connection, and besides this allows threads to work in parallel
                    var dbConnection = db.GetNewOpenConnection();
                    connection = new DatabaseConnectionWrapper(dbConnection);
                    connectionList.Add(db.ConnectionString, connection);
                }
                connection.AddRef();
            }

            return(connection);
        }
Ejemplo n.º 13
0
 public int UpdateDataSet(
     DataSet dataSet,
     string tableName,
     DbCommand insertCommand,
     DbCommand updateCommand,
     DbCommand deleteCommand,
     UpdateBehavior updateBehavior,
     int?updateBatchSize)
 {
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         if (updateBehavior == UpdateBehavior.Transactional && Transaction.Current == (Transaction)null)
         {
             DbTransaction transaction = Database.BeginTransaction(openConnection.Connection);
             try
             {
                 int num = this.UpdateDataSet(dataSet, tableName, insertCommand, updateCommand, deleteCommand, transaction, updateBatchSize);
                 Database.CommitTransaction((IDbTransaction)transaction);
                 return(num);
             }
             catch
             {
                 Database.RollbackTransaction((IDbTransaction)transaction);
                 throw;
             }
         }
         else
         {
             if (insertCommand != null)
             {
                 Database.PrepareCommand(insertCommand, openConnection.Connection);
             }
             if (updateCommand != null)
             {
                 Database.PrepareCommand(updateCommand, openConnection.Connection);
             }
             if (deleteCommand != null)
             {
                 Database.PrepareCommand(deleteCommand, openConnection.Connection);
             }
             return(this.DoUpdateDataSet(updateBehavior, dataSet, tableName, (IDbCommand)insertCommand, (IDbCommand)updateCommand, (IDbCommand)deleteCommand, updateBatchSize));
         }
     }
 }
        /// <summary>
        ///		Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
        ///		instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
        ///		returns null.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
                return null;

            Dictionary<string, DatabaseConnectionWrapper> connectionList;
            DatabaseConnectionWrapper connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    // We don't have a list for this transaction, so create a new one
                    connectionList = new Dictionary<string, DatabaseConnectionWrapper>();
                    transactionConnections.Add(currentTransaction, connectionList);

                    // We need to know when this previously unknown transaction is completed too
                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }

            lock (connectionList)
            {
                // Next we'll see if there is already a connection. If not, we'll create a new connection and add it
                // to the transaction's list of connections.
                // This collection should only be modified by the thread where the transaction scope was created
                // while the transaction scope is active.
                // However there's no documentation to confirm this, so we err on the safe side and lock.
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    // we're betting the cost of acquiring a new finer-grained lock is less than 
                    // that of opening a new connection, and besides this allows threads to work in parallel
                    var dbConnection = db.GetNewOpenConnection();
                    connection = new DatabaseConnectionWrapper(dbConnection);
                    connectionList.Add(db.ConnectionString, connection);
                }
                connection.AddRef();
            }

            return connection;
        }
Ejemplo n.º 15
0
 public void DiscoverParameters(DbCommand command)
 {
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     using (DatabaseConnectionWrapper openConnection = this.GetOpenConnection())
     {
         using (DbCommand commandByCommandType = this.CreateCommandByCommandType(command.CommandType, command.CommandText))
         {
             commandByCommandType.Connection = openConnection.Connection;
             this.DeriveParameters(commandByCommandType);
             foreach (ICloneable parameter in commandByCommandType.Parameters)
             {
                 IDataParameter dataParameter = (IDataParameter)parameter.Clone();
                 command.Parameters.Add((object)dataParameter);
             }
         }
     }
 }
 internal RefCountingOracleDataReaderWrapper(DatabaseConnectionWrapper connection, OracleDataReader innerReader)
     : base(innerReader)
 {
     this.connection = connection;
     this.connection.AddRef();
 }
Ejemplo n.º 17
0
 /// <summary>
 /// All data readers get wrapped in objects so that they properly manage connections.
 /// Some derived Database classes will need to create a different wrapper, so this
 /// method is provided so that they can do this.
 /// </summary>
 /// <param name="connection">Connection + refcount.</param>
 /// <param name="innerReader">The reader to wrap.</param>
 /// <returns>The new reader.</returns>
 protected virtual IDataReader CreateWrappedReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
 {
     return new RefCountingDataReader(connection, innerReader);
 }
Ejemplo n.º 18
0
        private int ExecuteNonQueryFanout(DatabaseConnectionWrapper wrapper, DbCommand command)
        {
            long? federationKey = 0;
               while (federationKey != null)
               {
               ExecuteFederationCommand(wrapper.Connection,FederationType.Member,federationKey,false);
               DoExecuteNonQueryWithRetry(command);
               using (DbCommand fCommand = _dbProviderFactory.CreateCommand())
               {
                   fCommand.CommandText = "SELECT CAST(range_high as bigint) FROM sys.federation_member_distributions";
                   PrepareCommand(fCommand,wrapper.Connection);
                   object key = DoExecuteScalarWithRetry(fCommand);
                   if (key != DBNull.Value)
                   {
                       federationKey = Convert.ToInt64(key);
                   }
                   else
                   {
                       federationKey = null;
                   }
               }

               }
               return 0;
        }
Ejemplo n.º 19
0
 protected virtual IDataReader CreateWrappedReader(
     DatabaseConnectionWrapper connection,
     IDataReader innerReader)
 {
     return((IDataReader) new RefCountingDataReader(connection, innerReader));
 }
Ejemplo n.º 20
0
 protected virtual DbDataReader CreateWrappedReaderForAsync(
     DatabaseConnectionWrapper connection,
     DbDataReader innerReader)
 {
     return((DbDataReader) new RefCountingDataReaderForAsync(connection, innerReader));
 }
 /// <summary>
 /// All data readers get wrapped in objects so that they properly manage connections.
 /// Some derived Database classes will need to create a different wrapper, so this
 /// method is provided so that they can do this.
 /// </summary>
 /// <param name="connection">Connection + refcount.</param>
 /// <param name="innerReader">The reader to wrap.</param>
 /// <returns>The new reader.</returns>
 protected override IDataReader CreateWrappedReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
 {
     // TODO : properly manage refcount
     //return new OracleDataReaderWrapper((OracleDataReader)innerReader);
     return new RefCountingOracleDataReaderWrapper(connection, (OracleDataReader)innerReader);
 }