Index: System.Data.SQLite/SQLite3.cs ================================================================== --- System.Data.SQLite/SQLite3.cs +++ System.Data.SQLite/SQLite3.cs @@ -286,10 +286,21 @@ internal override void ClearPool() { SQLiteConnectionPool.ClearPool(_fileName); } + + internal override int CountPool() + { + Dictionary counts = null; + int totalCount = 0; + + SQLiteConnectionPool.GetCounts(_fileName, + ref counts, ref totalCount); + + return totalCount; + } internal override void SetTimeout(int nTimeoutMS) { int n = UnsafeNativeMethods.sqlite3_busy_timeout(_sql, nTimeoutMS); if (n > 0) throw new SQLiteException(n, SQLiteLastError()); Index: System.Data.SQLite/SQLiteBase.cs ================================================================== --- System.Data.SQLite/SQLiteBase.cs +++ System.Data.SQLite/SQLiteBase.cs @@ -6,10 +6,11 @@ ********************************************************/ namespace System.Data.SQLite { using System; + using System.Collections.Generic; /// /// This internal class provides the foundation of SQLite support. It defines all the abstract members needed to implement /// a SQLite data provider, and inherits from SQLiteConvert which allows for simple translations of string to and from SQLite. /// @@ -85,10 +86,16 @@ /// /// When pooling is enabled, force this connection to be disposed rather than returned to the pool /// internal abstract void ClearPool(); + /// + /// When pooling is enabled, returns the number of pool entries matching the current file name. + /// + /// The number of pool entries matching the current file name. + internal abstract int CountPool(); + /// /// Prepares a SQL statement for execution. /// /// The source connection preparing the command. Can be null for any caller except LINQ /// The SQL command text to prepare Index: System.Data.SQLite/SQLiteConnection.cs ================================================================== --- System.Data.SQLite/SQLiteConnection.cs +++ System.Data.SQLite/SQLiteConnection.cs @@ -670,10 +670,22 @@ } _transactionLevel = 0; } OnStateChange(ConnectionState.Closed); } + + /// + /// Returns the number of pool entries for the file name associated with this connection. + /// + public int PoolCount + { + get + { + if (_sql == null) return 0; + return _sql.CountPool(); + } + } /// /// Clears the connection pool associated with the connection. Any other active connections using the same database file /// will be discarded instead of returned to the pool when they are closed. /// Index: System.Data.SQLite/SQLiteConnectionPool.cs ================================================================== --- System.Data.SQLite/SQLiteConnectionPool.cs +++ System.Data.SQLite/SQLiteConnectionPool.cs @@ -36,10 +36,60 @@ /// /// The default version number new pools will get /// private static int _poolVersion = 1; + + /// + /// Counts the number of pool entries matching the specified file name. + /// + /// The file name to match or null to match all files. + /// The pool entry counts for each matching file. + /// The total number of pool entries for all matching files. + internal static void GetCounts( + string fileName, + ref Dictionary counts, + ref int totalCount + ) + { + lock (_connections) + { + if (counts == null) + { + counts = new Dictionary( + StringComparer.OrdinalIgnoreCase); + } + + if (fileName != null) + { + Pool queue; + + if (_connections.TryGetValue(fileName, out queue)) + { + Queue poolQueue = queue.Queue; + int count = (poolQueue != null) ? poolQueue.Count : 0; + + counts.Add(fileName, count); + totalCount += count; + } + } + else + { + foreach (KeyValuePair pair in _connections) + { + if (pair.Value == null) + continue; + + Queue poolQueue = pair.Value.Queue; + int count = (poolQueue != null) ? poolQueue.Count : 0; + + counts.Add(pair.Key, count); + totalCount += count; + } + } + } + } /// /// Attempt to pull a pooled connection out of the queue for active duty /// /// The filename for a desired connection