/// <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're 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(); } }
// 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; }
/// <summary> /// Return a connection to the pool for someone else to use. /// </summary> /// <param name="fileName">The filename of the pool to use</param> /// <param name="hdl">The connection handle to pool</param> /// <param name="version">The pool version the handle was created under</param> /// <remarks> /// If the version numbers don't match between the connection and the pool, then the handle is discarded. /// </remarks> internal static void Add(string fileName, SqliteConnectionHandle hdl, int version) { lock (_connections) { // If the queue doesn't exist in the pool, then it must've been cleared sometime after the connection was created. Pool queue; if (_connections.TryGetValue(fileName, out queue) == true && version == queue.PoolVersion) { ResizePool(queue, true); queue.Queue.Enqueue(new WeakReference(hdl, false)); GC.KeepAlive(hdl); } else { hdl.Dispose(); } } }
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(); } } }
/// <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(); } } } } }
// 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; #if MONOTOUCH if (gch.IsAllocated) { gch.Free(); } #endif }
internal static void Close(this SqliteConnectionHandle connection) { connection.Dispose(); }