System.Data.SQLite
Check-in [35b8195272]
Not logged in

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

Overview
Comment:Commit any changes back to the original connection pool list prior to returning the connection handle from the pool.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 35b8195272bdfcadbd0fc79b0057ef3fb4723c8e
User & Date: mistachkin 2012-05-04 20:09:05
References
2012-06-01
19:42 Ticket [996d13cd87] access violation when recycling pooled connections status still Closed with 1 other change artifact: ef019df014 user: mistachkin
Context
2012-05-05
00:25
Update Eagle script library in externals to support logging of test setting overrides to the test log file. check-in: 62de6a5613 user: mistachkin tags: trunk
2012-05-04
20:09
Commit any changes back to the original connection pool list prior to returning the connection handle from the pool. check-in: 35b8195272 user: mistachkin tags: trunk
19:21
Modify test case for ticket [996d13cd87] to add some more stress testing and some additional diagnostic messages. check-in: 4faf9420bd user: mistachkin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

114
115
116
117
118
119
120

121
122
123
124
125
126
127
128
129


130
131
132
133
134
135
136
...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158








159



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220







































221
222
223
224
225
226
227
    /// </summary>
    /// <param name="fileName">The filename for a desired connection</param>
    /// <param name="maxPoolSize">The maximum size the connection pool for the filename can be</param>
    /// <param name="version">The pool version the returned connection will belong to</param>
    /// <returns>Returns NULL if no connections were available.  Even if none are, the poolversion will still be a valid pool version</returns>
    internal static SQLiteConnectionHandle Remove(string fileName, int maxPoolSize, out int version)
    {

        Queue<WeakReference> poolQueue;

        //
        // NOTE: This lock cannot be held while checking the queue for available
        //       connections because other methods of this class are called from
        //       the GC finalizer thread and we use the WaitForPendingFinalizers
        //       method (below).  Holding this lock while calling that method
        //       would therefore result in a deadlock.  This lock is held while
        //       a temporary copy of the queue is created.


        //
        lock (_connections)
        {
            Pool queue;

            // Default to the highest pool version
            version = _poolVersion;
................................................................................
                queue = new Pool(_poolVersion, maxPoolSize);
                _connections.Add(fileName, queue);

                return null;
            }

            // We found a pool for this file, so use its version number
            version = queue.PoolVersion;
            queue.MaxPoolSize = maxPoolSize;

            ResizePool(queue, false);

            // Try and get a pooled connection from the queue
            poolQueue = new Queue<WeakReference>(queue.Queue);
            if (poolQueue == null) return null;
        }












        while (poolQueue.Count > 0)
        {
            WeakReference cnn = poolQueue.Dequeue();
            if (cnn == null) continue;

            SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
            if (hdl == null) continue;

            //
            // BUGFIX: For ticket [996d13cd87], step #1.  After this point,
            //         make sure that the finalizer for the connection handle
            //         just obtained from the queue cannot START running (i.e.
            //         it may still be pending but it will no longer start
            //         after this point).
            //
            GC.SuppressFinalize(hdl);

            try
            {
                //
                // BUGFIX: For ticket [996d13cd87], step #2.  Now, we must wait
                //         for all pending finalizers which have STARTED running
                //         and have not yet COMPLETED.  This must be done just
                //         in case the finalizer for the connection handle just
                //         obtained from the queue has STARTED running at some
                //         point before SuppressFinalize was called on it.
                //
                //         After this point, checking properties of the
                //         connection handle (e.g. IsClosed) should work
                //         reliably without having to worry that they will
                //         (due to the finalizer) change out from under us.
                //
                GC.WaitForPendingFinalizers();

                //
                // BUGFIX: For ticket [996d13cd87], step #3.  Next, verify that
                //         the connection handle is actually valid and [still?]
                //         not closed prior to actually returning it to our
                //         caller.
                //
                if (!hdl.IsClosed && !hdl.IsInvalid)
                {
                    Interlocked.Increment(ref _poolOpened);
                    return hdl;
                }
            }
            finally
            {
                //
                // BUGFIX: For ticket [996d13cd87], step #4.  Finally, we must
                //         re-register the connection handle for finalization
                //         now that we have a strong reference to it (i.e. the
                //         finalizer run at least until the connection is
                //         subsequently closed).
                //
                GC.ReRegisterForFinalize(hdl);
            }

#pragma warning disable 162
            GC.KeepAlive(hdl); /* NOTE: Unreachable code. */
#pragma warning restore 162







































        }

        return null;
    }

    /// <summary>
    /// Clears out all pooled connections and rev's up the default pool version to force all old active objects







>







|
|
>
>







 







|





|

|
>
>
>
>
>
>
>
>
|
>
>
>
|
|
|
|

|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|


|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    /// </summary>
    /// <param name="fileName">The filename for a desired connection</param>
    /// <param name="maxPoolSize">The maximum size the connection pool for the filename can be</param>
    /// <param name="version">The pool version the returned connection will belong to</param>
    /// <returns>Returns NULL if no connections were available.  Even if none are, the poolversion will still be a valid pool version</returns>
    internal static SQLiteConnectionHandle Remove(string fileName, int maxPoolSize, out int version)
    {
        int localVersion;
        Queue<WeakReference> poolQueue;

        //
        // NOTE: This lock cannot be held while checking the queue for available
        //       connections because other methods of this class are called from
        //       the GC finalizer thread and we use the WaitForPendingFinalizers
        //       method (below).  Holding this lock while calling that method
        //       would therefore result in a deadlock.  Instead, this lock is
        //       held only while a temporary copy of the queue is created, and
        //       if necessary, when committing changes back to that original
        //       queue prior to returning from this method.
        //
        lock (_connections)
        {
            Pool queue;

            // Default to the highest pool version
            version = _poolVersion;
................................................................................
                queue = new Pool(_poolVersion, maxPoolSize);
                _connections.Add(fileName, queue);

                return null;
            }

            // We found a pool for this file, so use its version number
            version = localVersion = queue.PoolVersion;
            queue.MaxPoolSize = maxPoolSize;

            ResizePool(queue, false);

            // Try and get a pooled connection from the queue
            poolQueue = queue.Queue;
            if (poolQueue == null) return null;

            //
            // NOTE: Temporarily tranfer the queue for this file into a local
            //       variable.  The queue for this file will be modified and
            //       then committed back to the real pool list (below) prior
            //       to returning from this method.
            //
            _connections.Remove(fileName);
            poolQueue = new Queue<WeakReference>(poolQueue);
        }

        try
        {
            while (poolQueue.Count > 0)
            {
                WeakReference cnn = poolQueue.Dequeue();
                if (cnn == null) continue;

                SQLiteConnectionHandle hdl = cnn.Target as SQLiteConnectionHandle;
                if (hdl == null) continue;

                //
                // BUGFIX: For ticket [996d13cd87], step #1.  After this point,
                //         make sure that the finalizer for the connection
                //         handle just obtained from the queue cannot START
                //         running (i.e. it may still be pending but it will no
                //         longer start after this point).
                //
                GC.SuppressFinalize(hdl);

                try
                {
                    //
                    // BUGFIX: For ticket [996d13cd87], step #2.  Now, we must wait
                    //         for all pending finalizers which have STARTED running
                    //         and have not yet COMPLETED.  This must be done just
                    //         in case the finalizer for the connection handle just
                    //         obtained from the queue has STARTED running at some
                    //         point before SuppressFinalize was called on it.
                    //
                    //         After this point, checking properties of the
                    //         connection handle (e.g. IsClosed) should work
                    //         reliably without having to worry that they will
                    //         (due to the finalizer) change out from under us.
                    //
                    GC.WaitForPendingFinalizers();

                    //
                    // BUGFIX: For ticket [996d13cd87], step #3.  Next, verify that
                    //         the connection handle is actually valid and [still?]
                    //         not closed prior to actually returning it to our
                    //         caller.
                    //
                    if (!hdl.IsClosed && !hdl.IsInvalid)
                    {
                        Interlocked.Increment(ref _poolOpened);
                        return hdl;
                    }
                }
                finally
                {
                    //
                    // BUGFIX: For ticket [996d13cd87], step #4.  Next, we must
                    //         re-register the connection handle for finalization
                    //         now that we have a strong reference to it (i.e. the
                    //         finalizer run at least until the connection is
                    //         subsequently closed).
                    //
                    GC.ReRegisterForFinalize(hdl);
                }

#pragma warning disable 162
                GC.KeepAlive(hdl); /* NOTE: Unreachable code. */
#pragma warning restore 162
            }
        }
        finally
        {
            //
            // BUGFIX: For ticket [996d13cd87], step #5.  Finally, commit any
            //         changes to the pool/queue for this database file.
            //
            lock (_connections)
            {
                //
                // NOTE: We must check [again] if a pool exists for this file
                //       because one may have been added while the search for
                //       an available connection was in progress (above).
                //
                Pool queue;
                Queue<WeakReference> newPoolQueue;
                bool addPool;

                if (_connections.TryGetValue(fileName, out queue))
                {
                    addPool = false;
                }
                else
                {
                    addPool = true;
                    queue = new Pool(localVersion, maxPoolSize);
                }

                newPoolQueue = queue.Queue;

                while (poolQueue.Count > 0)
                    newPoolQueue.Enqueue(poolQueue.Dequeue());

                ResizePool(queue, false);

                if (addPool)
                    _connections.Add(fileName, queue);
            }
        }

        return null;
    }

    /// <summary>
    /// Clears out all pooled connections and rev's up the default pool version to force all old active objects