Example #1
0
        ///////////////////////////////////////////////////////////////////////

        /// <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);
                        }
                    }
                }
            }
        }
Example #2
0
        ///////////////////////////////////////////////////////////////////////

        /// <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);
            }
        }
Example #3
0
        ///////////////////////////////////////////////////////////////////////

        /// <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();
                }
            }
        }