示例#1
0
 private void ReleaseConnection(PooledDbConnection connection)
 {
     lock (_connectionPoolLock)
     {
         connection.IsBusy = false;
         _connectionPoolUpdated.Set();
     }
 }
示例#2
0
        private void OpenDatabase(string path)
        {
            this._mainConnection = new PooledDbConnection()
            {
                DbPointer = OpenDatabaseCore(path),
                IsBusy = false
            };

            if (path == ":memory:")
            {
                path = _memorySharedDatabasePath;
            }

            if (path.Contains(":memory:"))
            {
                SQLiteNativeMethods.sqlite3_update_hook(this._mainConnection.DbPointer, CacheManager.UpdateCallbackDelegate, this._mainConnection.DbPointer);
            }
        }
示例#3
0
 private void CloseDatabase()
 {
     this.CloseDatabaseCore(this._mainConnection.DbPointer);
     this._mainConnection = null;
     foreach (var conn in _connectionPool)
     {
         this.CloseDatabaseCore(conn.DbPointer);
     }
     _connectionPool.Clear();
     _connectionCount = 0;
 }
示例#4
0
        /// <summary>
        /// Wait for writes to finish, return first available read
        /// </summary>
        /// <returns></returns>
        private PooledDbConnection GetReadConnectionFromPool()
        {
            if (!_isInMemoryDatabase)
            {
                return _mainConnection;
            }

            if (_allowReadConnections)
            {
                lock (_connectionPoolLock)
                {
                    var pooledConnection = _connectionPool.FirstOrDefault(c => !c.IsBusy);
                    if (pooledConnection == null)
                    {
                        if (_connectionCount < _activeConnectionsMax)
                        {
                            pooledConnection = new PooledDbConnection()
                                {
                                    DbPointer = OpenDatabaseCore(_memorySharedDatabasePath),
                                    IsBusy = true
                                };

                            _connectionPool.Add(pooledConnection);

                            return pooledConnection;
                        }
                    }
                    _connectionPoolUpdated.Reset();
                }
            }

            _connectionPoolUpdated.WaitOne();
            return GetReadConnectionFromPool();
        }
示例#5
0
        public void Reload()
        {
            var conn = GetWriteConnection();

            IntPtr intPtr = OpenDatabaseCore(_memorySharedDatabasePath);
            this.PerformBackup(conn.DbPointer, intPtr);
            this.CloseDatabaseCore(conn.DbPointer);

            foreach (var pooledConnection in this._connectionPool) //TODO: sync
            {
                this.CloseDatabaseCore(pooledConnection.DbPointer);
            }
            _connectionPool.Clear();
            _connectionCount = 1;

            this._mainConnection = new PooledDbConnection()
            {
                DbPointer = intPtr,
                IsBusy = false
            };
        }