System.Data.SQLite
Check-in [9005890cba]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add PoolCount property to the SQLiteConnection class to return the number of pool entries for the file name associated with the connection.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tkt-996d13cd87
Files: files | file ages | folders
SHA1: 9005890cba181d062cda050b2181ebbba590f215
User & Date: mistachkin 2012-04-29 04:58:23
Context
2012-04-29
05:00
Remove superfluous using statement. check-in: 0b2bf29623 user: mistachkin tags: tkt-996d13cd87
04:58
Add PoolCount property to the SQLiteConnection class to return the number of pool entries for the file name associated with the connection. check-in: 9005890cba user: mistachkin tags: tkt-996d13cd87
03:27
Modify static IsAutocommit method to prevent it from passing IntPtr.Zero to the native library. check-in: 5dd7f49382 user: mistachkin tags: tkt-996d13cd87
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLite3.cs.

   284    284         SetTimeout(0);
   285    285       }
   286    286   
   287    287       internal override void ClearPool()
   288    288       {
   289    289         SQLiteConnectionPool.ClearPool(_fileName);
   290    290       }
          291  +
          292  +    internal override int CountPool()
          293  +    {
          294  +        Dictionary<string, int> counts = null;
          295  +        int totalCount = 0;
          296  +
          297  +        SQLiteConnectionPool.GetCounts(_fileName,
          298  +            ref counts, ref totalCount);
          299  +
          300  +        return totalCount;
          301  +    }
   291    302   
   292    303       internal override void SetTimeout(int nTimeoutMS)
   293    304       {
   294    305         int n = UnsafeNativeMethods.sqlite3_busy_timeout(_sql, nTimeoutMS);
   295    306         if (n > 0) throw new SQLiteException(n, SQLiteLastError());
   296    307       }
   297    308   

Changes to System.Data.SQLite/SQLiteBase.cs.

     4      4    * 
     5      5    * Released to the public domain, use at your own risk!
     6      6    ********************************************************/
     7      7   
     8      8   namespace System.Data.SQLite
     9      9   {
    10     10     using System;
           11  +  using System.Collections.Generic;
    11     12   
    12     13     /// <summary>
    13     14     /// This internal class provides the foundation of SQLite support.  It defines all the abstract members needed to implement
    14     15     /// a SQLite data provider, and inherits from SQLiteConvert which allows for simple translations of string to and from SQLite.
    15     16     /// </summary>
    16     17     internal abstract class SQLiteBase : SQLiteConvert, IDisposable
    17     18     {
................................................................................
    83     84       internal abstract string SQLiteLastError();
    84     85   
    85     86       /// <summary>
    86     87       /// When pooling is enabled, force this connection to be disposed rather than returned to the pool
    87     88       /// </summary>
    88     89       internal abstract void ClearPool();
    89     90   
           91  +    /// <summary>
           92  +    /// When pooling is enabled, returns the number of pool entries matching the current file name.
           93  +    /// </summary>
           94  +    /// <returns>The number of pool entries matching the current file name.</returns>
           95  +    internal abstract int CountPool();
           96  +
    90     97       /// <summary>
    91     98       /// Prepares a SQL statement for execution.
    92     99       /// </summary>
    93    100       /// <param name="cnn">The source connection preparing the command.  Can be null for any caller except LINQ</param>
    94    101       /// <param name="strSql">The SQL command text to prepare</param>
    95    102       /// <param name="previous">The previous statement in a multi-statement command, or null if no previous statement exists</param>
    96    103       /// <param name="timeoutMS">The timeout to wait before aborting the prepare</param>

Changes to System.Data.SQLite/SQLiteConnection.cs.

   668    668             _sql.Close();
   669    669             _sql = null;
   670    670           }
   671    671           _transactionLevel = 0;
   672    672         }
   673    673         OnStateChange(ConnectionState.Closed);
   674    674       }
          675  +
          676  +    /// <summary>
          677  +    /// Returns the number of pool entries for the file name associated with this connection.
          678  +    /// </summary>
          679  +    public int PoolCount
          680  +    {
          681  +        get
          682  +        {
          683  +            if (_sql == null) return 0;
          684  +            return _sql.CountPool();
          685  +        }
          686  +    }
   675    687   
   676    688       /// <summary>
   677    689       /// Clears the connection pool associated with the connection.  Any other active connections using the same database file
   678    690       /// will be discarded instead of returned to the pool when they are closed.
   679    691       /// </summary>
   680    692       /// <param name="connection"></param>
   681    693       public static void ClearPool(SQLiteConnection connection)

Changes to System.Data.SQLite/SQLiteConnectionPool.cs.

    34     34       /// </summary>
    35     35       private static SortedList<string, Pool> _connections = new SortedList<string, Pool>(StringComparer.OrdinalIgnoreCase);
    36     36   
    37     37       /// <summary>
    38     38       /// The default version number new pools will get
    39     39       /// </summary>
    40     40       private static int _poolVersion = 1;
           41  +
           42  +    /// <summary>
           43  +    /// Counts the number of pool entries matching the specified file name.
           44  +    /// </summary>
           45  +    /// <param name="fileName">The file name to match or null to match all files.</param>
           46  +    /// <param name="counts">The pool entry counts for each matching file.</param>
           47  +    /// <param name="totalCount">The total number of pool entries for all matching files.</param>
           48  +    internal static void GetCounts(
           49  +        string fileName,
           50  +        ref Dictionary<string, int> counts,
           51  +        ref int totalCount
           52  +        )
           53  +    {
           54  +        lock (_connections)
           55  +        {
           56  +            if (counts == null)
           57  +            {
           58  +                counts = new Dictionary<string, int>(
           59  +                    StringComparer.OrdinalIgnoreCase);
           60  +            }
           61  +
           62  +            if (fileName != null)
           63  +            {
           64  +                Pool queue;
           65  +
           66  +                if (_connections.TryGetValue(fileName, out queue))
           67  +                {
           68  +                    Queue<WeakReference> poolQueue = queue.Queue;
           69  +                    int count = (poolQueue != null) ? poolQueue.Count : 0;
           70  +
           71  +                    counts.Add(fileName, count);
           72  +                    totalCount += count;
           73  +                }
           74  +            }
           75  +            else
           76  +            {
           77  +                foreach (KeyValuePair<string, Pool> pair in _connections)
           78  +                {
           79  +                    if (pair.Value == null)
           80  +                        continue;
           81  +
           82  +                    Queue<WeakReference> poolQueue = pair.Value.Queue;
           83  +                    int count = (poolQueue != null) ? poolQueue.Count : 0;
           84  +
           85  +                    counts.Add(pair.Key, count);
           86  +                    totalCount += count;
           87  +                }
           88  +            }
           89  +        }
           90  +    }
    41     91   
    42     92       /// <summary>
    43     93       /// Attempt to pull a pooled connection out of the queue for active duty
    44     94       /// </summary>
    45     95       /// <param name="fileName">The filename for a desired connection</param>
    46     96       /// <param name="maxPoolSize">The maximum size the connection pool for the filename can be</param>
    47     97       /// <param name="version">The pool version the returned connection will belong to</param>