/// <summary>
        /// We don't have to thread-lock anything in this function, because it's only called by other functions above
        /// which already have a thread-safe lock.
        /// </summary>
        /// <param name="queue">The queue to resize</param>
        /// <param name="forAdding">If a function intends to add to the pool, this is true, which forces the resize
        /// to take one more than it needs from the pool</param>
        private static void ResizePool(Pool queue, bool forAdding)
        {
            int target = queue.MaxPoolSize;

            if (forAdding && target > 0)
            {
                target--;
            }

            Queue <WeakReference> poolQueue = queue.Queue;

            if (poolQueue == null)
            {
                return;
            }

            while (poolQueue.Count > target)
            {
                WeakReference cnn = poolQueue.Dequeue();
                if (cnn == null)
                {
                    continue;
                }
                SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                if (hdl != null)
                {
                    hdl.Dispose();
                }
                GC.KeepAlive(hdl);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Clears out all pooled connections and rev's up the default pool version to force all old active objects
        /// not in the pool to get discarded rather than returned to their pools.
        /// </summary>
        internal static void ClearAllPools()
        {
            lock (_connections)
            {
                foreach (KeyValuePair <string, Pool> pair in _connections)
                {
                    while (pair.Value.Queue.Count > 0)
                    {
                        WeakReference          cnn = pair.Value.Queue.Dequeue();
                        SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                        if (hdl != null)
                        {
                            hdl.Dispose();
                        }
                    }

                    // Keep track of the highest revision so we can go one higher when we're finished
                    if (_poolVersion <= pair.Value.PoolVersion)
                    {
                        _poolVersion = pair.Value.PoolVersion + 1;
                    }
                }
                // All pools are cleared and we have a new highest version number to force all old version active items to get discarded
                // instead of going back to the queue when they are closed.
                // We can get away with this because we've pumped up the _poolVersion out of range of all active connections, so they
                // will all get discarded when they try to put themselves back in their pool.
                _connections.Clear();
            }
        }
        /// <summary>
        /// Clear a given pool for a given filename.  Discards anything in the pool for the given file, and revs the pool
        /// version so current active objects on the old version of the pool will get discarded rather than be returned to the pool.
        /// </summary>
        /// <param name="fileName">The filename of the pool to clear</param>
        internal static void ClearPool(string fileName)
        {
            lock (_connections)
            {
                Pool queue;
                if (_connections.TryGetValue(fileName, out queue) == true)
                {
                    queue.PoolVersion++;

                    Queue <WeakReference> poolQueue = queue.Queue;
                    if (poolQueue == null)
                    {
                        return;
                    }

                    while (poolQueue.Count > 0)
                    {
                        WeakReference cnn = poolQueue.Dequeue();
                        if (cnn == null)
                        {
                            continue;
                        }
                        SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                        if (hdl != null)
                        {
                            hdl.Dispose();
                        }
                        GC.KeepAlive(hdl);
                    }
                }
            }
        }
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Disposes of all pooled connections associated with the specified
        /// database file name.
        /// </summary>
        /// <param name="fileName">
        /// The database file name.
        /// </param>
        internal static void ClearPool(string fileName)
        {
            ISQLiteConnectionPool connectionPool = GetConnectionPool();

            if (connectionPool != null)
            {
                connectionPool.ClearPool(fileName);
            }
            else
            {
                lock (_syncRoot)
                {
                    PoolQueue queue;

                    if (_queueList.TryGetValue(fileName, out queue))
                    {
                        queue.PoolVersion++;

                        Queue <WeakReference> poolQueue = queue.Queue;
                        if (poolQueue == null)
                        {
                            return;
                        }

                        while (poolQueue.Count > 0)
                        {
                            WeakReference connection = poolQueue.Dequeue();

                            if (connection == null)
                            {
                                continue;
                            }

                            SQLiteConnectionHandle handle =
                                connection.Target as SQLiteConnectionHandle;

                            if (handle != null)
                            {
                                handle.Dispose();
                            }

                            GC.KeepAlive(handle);
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private static void ResizePool(Pool queue, bool forAdding)
        {
            int maxPoolSize = queue.MaxPoolSize;

            if (forAdding && (maxPoolSize > 0))
            {
                maxPoolSize--;
            }
            while (queue.Queue.Count > maxPoolSize)
            {
                SQLiteConnectionHandle target = queue.Queue.Dequeue().Target as SQLiteConnectionHandle;
                if (target != null)
                {
                    target.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        // It isn't necessary to cleanup any functions we've registered.  If the connection
        // goes to the pool and is resurrected later, re-registered functions will overwrite the
        // previous functions.  The SQLiteFunctionCookieHandle will take care of freeing unmanaged
        // resources belonging to the previously-registered functions.
        internal override void Close()
        {
            if (_sql != null)
            {
                if (_usePool)
                {
                    SQLiteBase.ResetConnection(_sql);
                    SQLiteConnectionPool.Add(_fileName, _sql, _poolVersion);
                }
                else
                {
                    _sql.Dispose();
                }
            }

            _sql = null;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// We don't have to thread-lock anything in this function, because it's only called by other functions above
        /// which already have a thread-safe lock.
        /// </summary>
        /// <param name="queue">The queue to resize</param>
        /// <param name="forAdding">If a function intends to add to the pool, this is true, which forces the resize
        /// to take one more than it needs from the pool</param>
        private static void ResizePool(Pool queue, bool forAdding)
        {
            int target = queue.MaxPoolSize;

            if (forAdding && target > 0)
            {
                target--;
            }

            while (queue.Queue.Count > target)
            {
                WeakReference          cnn = queue.Queue.Dequeue();
                SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                if (hdl != null)
                {
                    hdl.Dispose();
                }
            }
        }
Ejemplo n.º 8
0
 internal static void ClearPool(string fileName)
 {
     lock (_connections)
     {
         Pool pool;
         if (_connections.TryGetValue(fileName, out pool))
         {
             pool.PoolVersion++;
             while (pool.Queue.Count > 0)
             {
                 SQLiteConnectionHandle target = pool.Queue.Dequeue().Target as SQLiteConnectionHandle;
                 if (target != null)
                 {
                     target.Dispose();
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Clear a given pool for a given filename.  Discards anything in the pool for the given file, and revs the pool
 /// version so current active objects on the old version of the pool will get discarded rather than be returned to the pool.
 /// </summary>
 /// <param name="fileName">The filename of the pool to clear</param>
 internal static void ClearPool(string fileName)
 {
     lock (_connections)
     {
         Pool queue;
         if (_connections.TryGetValue(fileName, out queue) == true)
         {
             queue.PoolVersion++;
             while (queue.Queue.Count > 0)
             {
                 WeakReference          cnn = queue.Queue.Dequeue();
                 SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                 if (hdl != null)
                 {
                     hdl.Dispose();
                 }
             }
         }
     }
 }
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// We do not have to thread-lock anything in this function, because it
        /// is only called by other functions above which already take the lock.
        /// </summary>
        /// <param name="queue">
        /// The pool queue to resize.
        /// </param>
        /// <param name="add">
        /// If a function intends to add to the pool, this is true, which
        /// forces the resize to take one more than it needs from the pool.
        /// </param>
        private static void ResizePool(
            PoolQueue queue,
            bool add
            )
        {
            int target = queue.MaxPoolSize;

            if (add && target > 0)
            {
                target--;
            }

            Queue <WeakReference> poolQueue = queue.Queue;

            if (poolQueue == null)
            {
                return;
            }

            while (poolQueue.Count > target)
            {
                WeakReference connection = poolQueue.Dequeue();

                if (connection == null)
                {
                    continue;
                }

                SQLiteConnectionHandle handle =
                    connection.Target as SQLiteConnectionHandle;

                if (handle != null)
                {
                    handle.Dispose();
                }

                GC.KeepAlive(handle);
            }
        }
Ejemplo n.º 11
0
 internal static void ClearAllPools()
 {
     lock (_connections)
     {
         foreach (KeyValuePair <string, Pool> pair in _connections)
         {
             while (pair.Value.Queue.Count > 0)
             {
                 SQLiteConnectionHandle target = pair.Value.Queue.Dequeue().Target as SQLiteConnectionHandle;
                 if (target != null)
                 {
                     target.Dispose();
                 }
             }
             if (_poolVersion <= pair.Value.PoolVersion)
             {
                 _poolVersion = pair.Value.PoolVersion + 1;
             }
         }
         _connections.Clear();
     }
 }
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Disposes of all pooled connections.
        /// </summary>
        internal static void ClearAllPools()
        {
            ISQLiteConnectionPool connectionPool = GetConnectionPool();

            if (connectionPool != null)
            {
                connectionPool.ClearAllPools();
            }
            else
            {
                lock (_syncRoot)
                {
                    foreach (KeyValuePair <string, PoolQueue> pair in _queueList)
                    {
                        if (pair.Value == null)
                        {
                            continue;
                        }

                        Queue <WeakReference> poolQueue = pair.Value.Queue;

                        while (poolQueue.Count > 0)
                        {
                            WeakReference connection = poolQueue.Dequeue();

                            if (connection == null)
                            {
                                continue;
                            }

                            SQLiteConnectionHandle handle =
                                connection.Target as SQLiteConnectionHandle;

                            if (handle != null)
                            {
                                handle.Dispose();
                            }

                            GC.KeepAlive(handle);
                        }

                        //
                        // NOTE: Keep track of the highest revision so we can
                        //       go one higher when we are finished.
                        //
                        if (_poolVersion <= pair.Value.PoolVersion)
                        {
                            _poolVersion = pair.Value.PoolVersion + 1;
                        }
                    }

                    //
                    // NOTE: All pools are cleared and we have a new highest
                    //       version number to force all old version active
                    //       items to get discarded instead of going back to
                    //       the queue when they are closed.  We can get away
                    //       with this because we have pumped up the pool
                    //       version out of range of all active connections,
                    //       so they will all get discarded when they try to
                    //       put themselves back into their pools.
                    //
                    _queueList.Clear();
                }
            }
        }