Ejemplo n.º 1
0
        /// <summary>
        /// Executes a query that returns the number of rows affected.
        /// This runs in blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        /// <param name="lastInsertedId">Contains the ID for the row that was inserted into the database. Only valid when the
        /// query contains an auto-increment column and the operation being performed is an insert.</param>
        /// <returns>The number of rows affected.</returns>
        public int ExecuteNonReaderWithResult(DbCommand cmd, DbQueryBase queryBase, out long lastInsertedId)
        {
            try
            {
                int ret;

                // Grab the execution lock since we are going to be executing queries
                lock (_executeQuerySync)
                {
                    // Empty out the queue
                    FlushQueue();

                    // Execute our job
                    ret = cmd.ExecuteNonQuery();

                    // Get the lastInsertedId
                    lastInsertedId = _dbConnectionPool.GetLastInsertedId(cmd);
                }

                return(ret);
            }
            finally
            {
                queryBase.ReleaseCommand(cmd);
            }
        }
        /// <summary>
        /// Creates a <see cref="DbQueryReaderDataReaderContainer"/>.
        /// </summary>
        /// <param name="dbQueryBase">The <see cref="DbQueryBase"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/>.</param>
        /// <param name="dataReader">The <see cref="IDataReader"/>.</param>
        /// <returns>The <see cref="DbQueryReaderDataReaderContainer"/> instance.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dbQueryBase" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="command" /> is <c>null</c>.</exception>
        public static DbQueryReaderDataReaderContainer Create(DbQueryBase dbQueryBase, DbCommand command, IDataReader dataReader)
        {
            if (dbQueryBase == null)
                throw new ArgumentNullException("dbQueryBase");
            if (command == null)
                throw new ArgumentNullException("command");

            DbQueryReaderDataReaderContainer r = null;

            // Try to grab from the pool first
            lock (_poolSync)
            {
                if (_pool.Count > 0)
                    r = _pool.Pop();
            }

            // Couldn't grab from pool - create new instance
            if (r == null)
                r = new DbQueryReaderDataReaderContainer();

            Debug.Assert(r._dbQueryBase == null);
            Debug.Assert(r._command == null);
            Debug.Assert(r.DataReader == null);

            // Initialize
            r._dbQueryBase = dbQueryBase;
            r._command = command;
            r.DataReader = dataReader;

            return r;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes a query that has no return value.
        /// This runs in non-blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        public void ExecuteNonReader(DbCommand cmd, DbQueryBase queryBase)
        {
            var v = new QueueItem(cmd, queryBase);

            // Simply push the job into the queue
            lock (_queueSync)
            {
                _queue.Enqueue(v);
            }
        }
        protected override void Dispose(bool disposeManaged)
        {
            try
            {
                if (DataReader != null)
                {
                    DataReader.Dispose();
                }
            }
            finally
            {
                try
                {
                    _dbQueryBase.ConnectionPool.QueryRunner.EndExecuteReader();
                }
                catch (SynchronizationLockException ex)
                {
                    const string errmsg = "Failed to release reader on `{0}` for query `{1}`. Exception: {2}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, this, _command, ex);
                    }
                    Debug.Fail(string.Format(errmsg, this, _command, ex));
                }
                finally
                {
                    try
                    {
                        _dbQueryBase.ReleaseCommand(_command);
                    }
                    finally
                    {
                        // Release the object references
                        _command     = null;
                        _dbQueryBase = null;
                        DataReader   = null;

                        // Throw back into the pool
                        lock (_poolSync)
                        {
                            Debug.Assert(!_pool.Contains(this));
                            _pool.Push(this);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Creates a <see cref="DbQueryReaderDataReaderContainer"/>.
        /// </summary>
        /// <param name="dbQueryBase">The <see cref="DbQueryBase"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/>.</param>
        /// <param name="dataReader">The <see cref="IDataReader"/>.</param>
        /// <returns>The <see cref="DbQueryReaderDataReaderContainer"/> instance.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dbQueryBase" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="command" /> is <c>null</c>.</exception>
        public static DbQueryReaderDataReaderContainer Create(DbQueryBase dbQueryBase, DbCommand command, IDataReader dataReader)
        {
            if (dbQueryBase == null)
            {
                throw new ArgumentNullException("dbQueryBase");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            DbQueryReaderDataReaderContainer r = null;

            // Try to grab from the pool first
            lock (_poolSync)
            {
                if (_pool.Count > 0)
                {
                    r = _pool.Pop();
                }
            }

            // Couldn't grab from pool - create new instance
            if (r == null)
            {
                r = new DbQueryReaderDataReaderContainer();
            }

            Debug.Assert(r._dbQueryBase == null);
            Debug.Assert(r._command == null);
            Debug.Assert(r.DataReader == null);

            // Initialize
            r._dbQueryBase = dbQueryBase;
            r._command     = command;
            r.DataReader   = dataReader;

            return(r);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes a query that returns the number of rows affected.
        /// This runs in blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        /// <returns>The number of rows affected.</returns>
        public int ExecuteNonReaderWithResult(DbCommand cmd, DbQueryBase queryBase)
        {
            try
            {
                int ret;

                // Grab the execution lock since we are going to be executing queries
                lock (_executeQuerySync)
                {
                    // Empty out the queue
                    FlushQueue();

                    // Execute our job
                    ret = cmd.ExecuteNonQuery();
                }

                return(ret);
            }
            finally
            {
                queryBase.ReleaseCommand(cmd);
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QueueItem"/> struct.
 /// </summary>
 /// <param name="command">The <see cref="DbCommand"/>.</param>
 /// <param name="query">The <see cref="DbQueryBase"/>.</param>
 public QueueItem(DbCommand command, DbQueryBase query)
 {
     _command = command;
     _query = query;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Executes a query that returns the number of rows affected.
        /// This runs in blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        /// <param name="lastInsertedId">Contains the ID for the row that was inserted into the database. Only valid when the
        /// query contains an auto-increment column and the operation being performed is an insert.</param>
        /// <returns>The number of rows affected.</returns>
        public int ExecuteNonReaderWithResult(DbCommand cmd, DbQueryBase queryBase, out long lastInsertedId)
        {
            try
            {
                int ret;

                // Grab the execution lock since we are going to be executing queries
                lock (_executeQuerySync)
                {
                    // Empty out the queue
                    FlushQueue();

                    // Execute our job
                    ret = cmd.ExecuteNonQuery();

                    // Get the lastInsertedId
                    lastInsertedId = _dbConnectionPool.GetLastInsertedId(cmd);
                }

                return ret;
            }
            finally
            {
                queryBase.ReleaseCommand(cmd);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Executes a query that returns the number of rows affected.
        /// This runs in blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        /// <returns>The number of rows affected.</returns>
        public int ExecuteNonReaderWithResult(DbCommand cmd, DbQueryBase queryBase)
        {
            try
            {
                int ret;

                // Grab the execution lock since we are going to be executing queries
                lock (_executeQuerySync)
                {
                    // Empty out the queue
                    FlushQueue();

                    // Execute our job
                    ret = cmd.ExecuteNonQuery();
                }

                return ret;
            }
            finally
            {
                queryBase.ReleaseCommand(cmd);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Executes a query that has no return value.
        /// This runs in non-blocking mode.
        /// </summary>
        /// <param name="cmd">The <see cref="DbCommand"/> to execute.</param>
        /// <param name="queryBase">The <see cref="DbQueryBase"/> that is executing this command.</param>
        public void ExecuteNonReader(DbCommand cmd, DbQueryBase queryBase)
        {
            var v = new QueueItem(cmd, queryBase);

            // Simply push the job into the queue
            lock (_queueSync)
            {
                _queue.Enqueue(v);
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QueueItem"/> struct.
 /// </summary>
 /// <param name="command">The <see cref="DbCommand"/>.</param>
 /// <param name="query">The <see cref="DbQueryBase"/>.</param>
 public QueueItem(DbCommand command, DbQueryBase query)
 {
     _command = command;
     _query   = query;
 }
        protected override void Dispose(bool disposeManaged)
        {
            try
            {
                if (DataReader != null)
                    DataReader.Dispose();
            }
            finally
            {
                try
                {
                    _dbQueryBase.ConnectionPool.QueryRunner.EndExecuteReader();
                }
                catch (SynchronizationLockException ex)
                {
                    const string errmsg = "Failed to release reader on `{0}` for query `{1}`. Exception: {2}";
                    if (log.IsErrorEnabled)
                        log.ErrorFormat(errmsg, this, _command, ex);
                    Debug.Fail(string.Format(errmsg, this, _command, ex));
                }
                finally
                {
                    try
                    {
                        _dbQueryBase.ReleaseCommand(_command);
                    }
                    finally
                    {
                        // Release the object references
                        _command = null;
                        _dbQueryBase = null;
                        DataReader = null;

                        // Throw back into the pool
                        lock (_poolSync)
                        {
                            Debug.Assert(!_pool.Contains(this));
                            _pool.Push(this);
                        }
                    }
                }
            }
        }