Beispiel #1
0
        private MySqlInternalConnection GetPooledConnection()
        {
            MySqlInternalConnection conn = null;

            // if there are no idle connections and the in use pool is full
            // then return null to indicate that we cannot provide a connection
            // at this time.
            if (idlePool.Count == 0 && inUsePool.Count == maxSize)
            {
                return(null);
            }

            lock (idlePool.SyncRoot)
            {
                for (int i = idlePool.Count - 1; i >= 0; i--)
                {
                    conn = (idlePool[i] as MySqlInternalConnection);
                    if (conn.IsAlive())
                    {
                        lock (inUsePool)
                        {
                            inUsePool.Add(conn);
                        }
                        idlePool.RemoveAt(i);
                        return(conn);
                    }
                    else
                    {
                        conn.Close();
                        idlePool.RemoveAt(i);
                        conn = null;
                    }
                }
            }

            // if we couldn't get a pooled connection and there is still room
            // make a new one
            if (conn == null && (idlePool.Count + inUsePool.Count) < maxSize)
            {
                conn = CreateNewPooledConnection();
                if (conn == null)
                {
                    return(null);
                }

                lock (idlePool.SyncRoot)
                    lock (inUsePool.SyncRoot)
                    {
                        idlePool.Remove(conn);
                        inUsePool.Add(conn);
                    }
            }

            return(conn);
        }
Beispiel #2
0
 public void ReleaseConnection(MySqlInternalConnection connection)
 {
     lock (idlePool.SyncRoot)
         lock (inUsePool.SyncRoot)
         {
             inUsePool.Remove(connection);
             if (connection.Settings.ConnectionLifetime != 0 && connection.IsTooOld())
             {
                 connection.Close();
             }
             else
             {
                 idlePool.Add(connection);
             }
         }
 }
Beispiel #3
0
        /// <summary>
        /// Closes the connection to the database. This is the preferred method of closing any open connection.
        /// </summary>
        public void Close()
        {
            if (state == ConnectionState.Closed)
            {
                return;
            }

            if (dataReader != null)
            {
                dataReader.Close();
            }

            if (settings.Pooling)
            {
                MySqlPoolManager.ReleaseConnection(internalConnection);
            }
            else
            {
                internalConnection.Close();
            }

            SetState(ConnectionState.Closed);
        }
		public void ReleaseConnection( MySqlInternalConnection connection )
		{
			lock (idlePool.SyncRoot)
				lock (inUsePool.SyncRoot) 
				{
					inUsePool.Remove( connection );
					if (connection.Settings.ConnectionLifetime != 0 && connection.IsTooOld())
						connection.Close();
					else
						idlePool.Add( connection );
				}
		}