Esempio n. 1
0
        private IDbConnection GetPoolConnection()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("SqlStoreBase");
            }

            lock (_connectionPool)
            {
                IDbConnection connection;

                do
                {
                    connection = GetFreeConnectionInPool();

                    if (connection != null)
                    {
                        if (Open(connection))
                        {
                            return(connection);
                        }

                        // Broken connection, maybe disposed
                        _connectionPool.Remove(connection);
                    }

                    if (_connectionPool.Count < ConnectionPoolSize)
                    {
                        connection = _dbEngine.GetNewConnection();
                        connection.Open();
                        _connectionPool.Add(connection);
                        Interlocked.Increment(ref _connectionCount);
                        OrmDebug.Trace("Creating pooled connection");
                        return(connection);
                    }

                    // pool is full, we have to wait
                    Thread.Sleep(1000);

                    // TODO: add a timeout?
                } while (connection == null);

                // this should never happen
                throw new TimeoutException("Unable to get a pooled connection.");
            }
        }
Esempio n. 2
0
        public IDbConnection GetConnection()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("ConnectionPool");
            }

            lock (_pool)
            {
                IDbConnection connection;

                do
                {
                    connection = GetFreeConnectionInPool();

                    if (connection != null)
                    {
                        if (Open(connection))
                        {
                            return(connection);
                        }

                        // Broken connection, maybe disposed
                        connection.Dispose();
                        _pool.Remove(connection);
                    }

                    if (_pool.Count < ConnectionPoolSize)
                    {
                        connection = _dbEngine.GetNewConnection();
                        connection.Open();
                        _pool.Add(connection);
                        OrmDebug.Trace("Creating pooled connection");
                        return(connection);
                    }

                    OrmDebug.Trace("Pool full waiting for free connection");
                    Thread.Sleep(1000);

                    // TODO: add a timeout?
                } while (connection == null);

                throw new TimeoutException("Unable to get a pooled connection.");
            }
        }