internal static void Add(string fileName, SQLiteConnectionHandle handle, int version) { SQLiteConnectionPool.PoolQueue poolQueue; ISQLiteConnectionPool connectionPool = SQLiteConnectionPool.GetConnectionPool(); if (connectionPool != null) { connectionPool.Add(fileName, handle, version); return; } lock (SQLiteConnectionPool._syncRoot) { if (!SQLiteConnectionPool._queueList.TryGetValue(fileName, out poolQueue) || version != poolQueue.PoolVersion) { handle.Close(); } else { SQLiteConnectionPool.ResizePool(poolQueue, true); Queue <WeakReference> queue = poolQueue.Queue; if (queue != null) { queue.Enqueue(new WeakReference(handle, false)); Interlocked.Increment(ref SQLiteConnectionPool._poolClosed); } else { return; } } GC.KeepAlive(handle); } }
internal static void ClearAllPools() { ISQLiteConnectionPool connectionPool = SQLiteConnectionPool.GetConnectionPool(); if (connectionPool != null) { connectionPool.ClearAllPools(); return; } lock (SQLiteConnectionPool._syncRoot) { foreach (KeyValuePair <string, SQLiteConnectionPool.PoolQueue> keyValuePair in SQLiteConnectionPool._queueList) { if (keyValuePair.Value == null) { continue; } Queue <WeakReference> queue = keyValuePair.Value.Queue; while (queue.Count > 0) { WeakReference weakReference = queue.Dequeue(); if (weakReference == null) { continue; } SQLiteConnectionHandle target = weakReference.Target as SQLiteConnectionHandle; if (target != null) { target.Dispose(); } GC.KeepAlive(target); } if (SQLiteConnectionPool._poolVersion > keyValuePair.Value.PoolVersion) { continue; } SQLiteConnectionPool._poolVersion = keyValuePair.Value.PoolVersion + 1; } SQLiteConnectionPool._queueList.Clear(); } }
internal static void GetCounts(string fileName, ref Dictionary <string, int> counts, ref int openCount, ref int closeCount, ref int totalCount) { SQLiteConnectionPool.PoolQueue poolQueue; ISQLiteConnectionPool connectionPool = SQLiteConnectionPool.GetConnectionPool(); if (connectionPool != null) { connectionPool.GetCounts(fileName, ref counts, ref openCount, ref closeCount, ref totalCount); return; } lock (SQLiteConnectionPool._syncRoot) { openCount = SQLiteConnectionPool._poolOpened; closeCount = SQLiteConnectionPool._poolClosed; if (counts == null) { counts = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase); } if (fileName == null) { foreach (KeyValuePair <string, SQLiteConnectionPool.PoolQueue> keyValuePair in SQLiteConnectionPool._queueList) { if (keyValuePair.Value == null) { continue; } Queue <WeakReference> queue = keyValuePair.Value.Queue; int num = (queue != null ? queue.Count : 0); counts.Add(keyValuePair.Key, num); totalCount += num; } } else if (SQLiteConnectionPool._queueList.TryGetValue(fileName, out poolQueue)) { Queue <WeakReference> weakReferences = poolQueue.Queue; int num1 = (weakReferences != null ? weakReferences.Count : 0); counts.Add(fileName, num1); totalCount += num1; } } }
internal static void ClearPool(string fileName) { SQLiteConnectionPool.PoolQueue poolQueue; ISQLiteConnectionPool connectionPool = SQLiteConnectionPool.GetConnectionPool(); if (connectionPool != null) { connectionPool.ClearPool(fileName); return; } lock (SQLiteConnectionPool._syncRoot) { if (SQLiteConnectionPool._queueList.TryGetValue(fileName, out poolQueue)) { poolQueue.PoolVersion++; Queue <WeakReference> queue = poolQueue.Queue; if (queue != null) { while (queue.Count > 0) { WeakReference weakReference = queue.Dequeue(); if (weakReference == null) { continue; } SQLiteConnectionHandle target = weakReference.Target as SQLiteConnectionHandle; if (target != null) { target.Dispose(); } GC.KeepAlive(target); } } else { return; } } } }
internal static SQLiteConnectionHandle Remove(string fileName, int maxPoolSize, out int version) { int num; Queue <WeakReference> queue; SQLiteConnectionPool.PoolQueue poolQueue; SQLiteConnectionPool.PoolQueue poolQueue1; bool flag; SQLiteConnectionHandle sQLiteConnectionHandle; ISQLiteConnectionPool connectionPool = SQLiteConnectionPool.GetConnectionPool(); if (connectionPool != null) { return(connectionPool.Remove(fileName, maxPoolSize, out version) as SQLiteConnectionHandle); } lock (SQLiteConnectionPool._syncRoot) { version = SQLiteConnectionPool._poolVersion; if (SQLiteConnectionPool._queueList.TryGetValue(fileName, out poolQueue)) { int poolVersion = poolQueue.PoolVersion; num = poolVersion; version = poolVersion; poolQueue.MaxPoolSize = maxPoolSize; SQLiteConnectionPool.ResizePool(poolQueue, false); queue = poolQueue.Queue; if (queue != null) { SQLiteConnectionPool._queueList.Remove(fileName); queue = new Queue <WeakReference>(queue); goto Label0; } else { sQLiteConnectionHandle = null; } } else { poolQueue = new SQLiteConnectionPool.PoolQueue(SQLiteConnectionPool._poolVersion, maxPoolSize); SQLiteConnectionPool._queueList.Add(fileName, poolQueue); sQLiteConnectionHandle = null; } } return(sQLiteConnectionHandle); Label0: try { while (queue.Count > 0) { WeakReference weakReference = queue.Dequeue(); if (weakReference == null) { continue; } SQLiteConnectionHandle target = weakReference.Target as SQLiteConnectionHandle; if (target == null) { continue; } GC.SuppressFinalize(target); try { GC.WaitForPendingFinalizers(); if (!target.IsInvalid && !target.IsClosed) { Interlocked.Increment(ref SQLiteConnectionPool._poolOpened); sQLiteConnectionHandle = target; return(sQLiteConnectionHandle); } } finally { GC.ReRegisterForFinalize(target); } GC.KeepAlive(target); } } finally { lock (SQLiteConnectionPool._syncRoot) { if (!SQLiteConnectionPool._queueList.TryGetValue(fileName, out poolQueue1)) { flag = true; poolQueue1 = new SQLiteConnectionPool.PoolQueue(num, maxPoolSize); } else { flag = false; } Queue <WeakReference> weakReferences = poolQueue1.Queue; while (queue.Count > 0) { weakReferences.Enqueue(queue.Dequeue()); } SQLiteConnectionPool.ResizePool(poolQueue1, false); if (flag) { SQLiteConnectionPool._queueList.Add(fileName, poolQueue1); } } } return(null); }
internal override void Open(string strFilename, string vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool) { SQLiteErrorCode sQLiteErrorCode; if (this._sql != null) { this.Close(true); } if (this._sql != null) { throw new SQLiteException("connection handle is still active"); } this._usePool = usePool; this._fileName = strFilename; this._flags = connectionFlags; if (usePool) { this._sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out this._poolVersion); SQLiteConnectionHandle sQLiteConnectionHandle = this._sql; object[] objArray = new object[] { typeof(SQLite3_UTF16), strFilename, vfsName, connectionFlags, openFlags, maxPoolSize, usePool, this._poolVersion }; SQLiteConnection.OnChanged(null, new ConnectionEventArgs(SQLiteConnectionEventType.OpenedFromPool, null, null, null, null, sQLiteConnectionHandle, strFilename, objArray)); } if (this._sql == null) { try { } finally { IntPtr zero = IntPtr.Zero; int num = ((connectionFlags & SQLiteConnectionFlags.NoExtensionFunctions) != SQLiteConnectionFlags.NoExtensionFunctions ? 1 : 0); if (vfsName != null || num != 0) { sQLiteErrorCode = UnsafeNativeMethods.sqlite3_open16_interop(SQLiteConvert.ToUTF8(strFilename), SQLiteConvert.ToUTF8(vfsName), openFlags, num, ref zero); } else { if ((openFlags & SQLiteOpenFlagsEnum.Create) != SQLiteOpenFlagsEnum.Create && !File.Exists(strFilename)) { throw new SQLiteException(SQLiteErrorCode.CantOpen, strFilename); } if (vfsName != null) { CultureInfo currentCulture = CultureInfo.CurrentCulture; object[] objArray1 = new object[] { vfsName }; throw new SQLiteException(SQLiteErrorCode.CantOpen, HelperMethods.StringFormat(currentCulture, "cannot open using UTF-16 and VFS \"{0}\": need interop assembly", objArray1)); } sQLiteErrorCode = UnsafeNativeMethods.sqlite3_open16(strFilename, ref zero); } if (sQLiteErrorCode != SQLiteErrorCode.Ok) { throw new SQLiteException(sQLiteErrorCode, null); } this._sql = new SQLiteConnectionHandle(zero, true); } lock (this._sql) { } SQLiteConnectionHandle sQLiteConnectionHandle1 = this._sql; object[] objArray2 = new object[] { typeof(SQLite3_UTF16), strFilename, vfsName, connectionFlags, openFlags, maxPoolSize, usePool }; SQLiteConnection.OnChanged(null, new ConnectionEventArgs(SQLiteConnectionEventType.NewCriticalHandle, null, null, null, null, sQLiteConnectionHandle1, strFilename, objArray2)); } if ((connectionFlags & SQLiteConnectionFlags.NoBindFunctions) != SQLiteConnectionFlags.NoBindFunctions) { if (this._functions == null) { this._functions = new Dictionary <SQLiteFunctionAttribute, SQLiteFunction>(); } foreach (KeyValuePair <SQLiteFunctionAttribute, SQLiteFunction> value in SQLiteFunction.BindFunctions(this, connectionFlags)) { this._functions[value.Key] = value.Value; } } this.SetTimeout(0); GC.KeepAlive(this._sql); }