internal void HandleTimeoutOrThreadAbort(Exception ex)
        {
            bool isFatal = false;

            if (isKillQueryConnection)
            {
                // Special connection started to cancel a query.
                // Abort will prevent recursive connection spawning
                Abort();
                if (ex is TimeoutException)
                {
                    throw new MySqlException(Resources.Timeout, true, ex);
                }
                else
                {
                    return;
                }
            }

            try
            {
                // Do a fast cancel.The reason behind small values for connection
                // and command timeout is that we do not want user to wait longer
                // after command has already expired.
                // Microsoft's SqlClient seems to be using 5 seconds timeouts
                // here as well.
                // Read the  error packet with "interrupted" message.
                CancelQuery(5);
                driver.ResetTimeout(5000);
                if (Reader != null)
                {
                    Reader.Close();
                    Reader = null;
                }
            }
            catch (Exception ex2)
            {
                MySqlTrace.LogWarning(ServerThread, "Could not kill query, " +
                                      " aborting connection. Exception was " + ex2.Message);
                Abort();
                isFatal = true;
            }
            if (ex is TimeoutException)
            {
                throw new MySqlException(Resources.Timeout, isFatal, ex);
            }
        }
Example #2
0
        /// <summary>
        /// It is assumed that this method is only called from inside an active lock.
        /// </summary>
        private Driver GetPooledConnection()
        {
            Driver driver = null;

            // if we don't have an idle connection but we have room for a new
            // one, then create it here.
            lock ((idlePool as ICollection).SyncRoot)
            {
                if (HasIdleConnections)
                {
                    driver = idlePool.Dequeue();
                }
            }

            // Obey the connection timeout
            if (driver != null)
            {
                try
                {
                    driver.ResetTimeout((int)Settings.ConnectionTimeout * 1000);
                }
                catch (Exception)
                {
                    driver.Close();
                    driver = null;
                }
            }

            if (driver != null)
            {
                // first check to see that the server is still alive
                if (!driver.Ping())
                {
                    driver.Close();
                    driver = null;
                }
                else if (settings.ConnectionReset)
                {
                    // if the user asks us to ping/reset pooled connections
                    // do so now
                    driver.Reset();
                }
            }
            if (driver == null)
            {
                driver = CreateNewPooledConnection();
            }

            Debug.Assert(driver != null);
            lock ((inUsePool as ICollection).SyncRoot)
            {
                inUsePool.Add(driver);
            }
            return(driver);
        }