internal void CloseFully()
        {
            if (settings.Pooling && driver.IsOpen)
            {
                // if we are in a transaction, roll it back
                if (driver.HasStatus(ServerStatusFlags.InTransaction))
                {
                    MySqlTransaction t = new MySqlTransaction(this, IsolationLevel.Unspecified);
                    t.Rollback();
                }

                MySqlPoolManager.ReleaseConnection(driver);
            }
            else
            {
                driver.Close();
            }
            driver = null;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes a connection from the in use pool.  The only situations where this method
        /// would be called are when a connection that is in use gets some type of fatal exception
        /// or when the connection is being returned to the pool and it's too old to be
        /// returned.
        /// </summary>
        /// <param name="driver"></param>
        public void RemoveConnection(Driver driver)
        {
            lock ((inUsePool as ICollection).SyncRoot)
            {
                if (inUsePool.Contains(driver))
                {
                    inUsePool.Remove(driver);
                    Interlocked.Increment(ref available);
                    autoEvent.Set();
                }
            }

            // if we are being cleared and we are out of connections then have
            // the manager destroy us.
            if (beingCleared && NumConnections == 0)
            {
                MySqlPoolManager.RemoveClearedPool(this);
            }
        }
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                throw new InvalidOperationException(ResourceStrings.ConnectionAlreadyOpen);
            }
            SetState(ConnectionState.Connecting, true);
            try
            {
                if (settings.Pooling)
                {
                    MySqlPool pool = MySqlPoolManager.GetPool(settings);
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = pool.GetConnection();
                    }
                }
                else
                {
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = Driver.Create(settings);
                    }
                }
            }
            catch (Exception)
            {
                SetState(ConnectionState.Closed, true);
                throw;
            }

            SetState(ConnectionState.Open, false);
            driver.Configure(this);
            if (settings.Database != null && settings.Database != String.Empty)
            {
                ChangeDatabase(settings.Database);
            }
            hasBeenOpen = true;
            SetState(ConnectionState.Open, true);
        }
Ejemplo n.º 4
0
        protected virtual void Dispose(bool disposing)
        {
            // Avoid cyclic calls to Dispose.
            if (disposeInProgress)
            {
                return;
            }

            disposeInProgress = true;

            try
            {
                ResetTimeout(1000);
                if (disposing)
                {
                    handler.Close(isOpen);
                }
                // if we are pooling, then release ourselves
                if (connectionString.Pooling)
                {
                    MySqlPoolManager.RemoveConnection(this);
                }
            }
            catch (Exception)
            {
                if (disposing)
                {
                    throw;
                }
            }
            finally
            {
                reader            = null;
                isOpen            = false;
                disposeInProgress = false;
            }
        }
 /// <include file='docs/MySqlConnection.xml' path='docs/ClearAllPools/*'/>
 public static void ClearAllPools()
 {
     MySqlPoolManager.ClearAllPools();
 }
        /*
         * // Due to the DNXCore replacement for DataReader.GetSchemaTable()
         * // haven't implemented (refer to https://github.com/dotnet/corefx/issues/3423)
         * // this method should be remove till GetSchema is back.
         * //
         *
         * /// <summary>
         * /// Returns schema information for the data source of this <see cref="DbConnection"/>.
         * /// </summary>
         * /// <returns>A <see cref="DataTable"/> that contains schema information. </returns>
         * public override DataTable GetSchema()
         * {
         *  return GetSchema(null);
         * }
         *
         * /// <summary>
         * /// Returns schema information for the data source of this
         * /// <see cref="DbConnection"/> using the specified string for the schema name.
         * /// </summary>
         * /// <param name="collectionName">Specifies the name of the schema to return. </param>
         * /// <returns>A <see cref="DataTable"/> that contains schema information. </returns>
         * public override DataTable GetSchema(string collectionName)
         * {
         *  if (collectionName == null)
         *      collectionName = SchemaProvider.MetaCollection;
         *
         *  return GetSchema(collectionName, null);
         * }
         *
         * /// <summary>
         * /// Returns schema information for the data source of this <see cref="DbConnection"/>
         * /// using the specified string for the schema name and the specified string array
         * /// for the restriction values.
         * /// </summary>
         * /// <param name="collectionName">Specifies the name of the schema to return.</param>
         * /// <param name="restrictionValues">Specifies a set of restriction values for the requested schema.</param>
         * /// <returns>A <see cref="DataTable"/> that contains schema information.</returns>
         * public override DataTable GetSchema(string collectionName, string[] restrictionValues)
         * {
         *  if (collectionName == null)
         *      collectionName = SchemaProvider.MetaCollection;
         *
         *  string[] restrictions = schemaProvider.CleanRestrictions(restrictionValues);
         *  DataTable dt = schemaProvider.GetSchema(collectionName, restrictions);
         *  return dt;
         * }
         */

        /// <include file='docs/MySqlConnection.xml' path='docs/ClearPool/*'/>
        public static void ClearPool(MySqlConnection connection)
        {
            MySqlPoolManager.ClearPool(connection.Settings);
        }
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                throw new InvalidOperationException(Resources.ConnectionAlreadyOpen);
            }

            SetState(ConnectionState.Connecting, true);

#if !CF
            // if we are auto enlisting in a current transaction, then we will be
            // treating the connection as pooled
            if (settings.AutoEnlist && Transaction.Current != null)
            {
                driver = DriverTransactionManager.GetDriverInTransaction(Transaction.Current);
                if (driver != null &&
                    (driver.IsInActiveUse ||
                     !driver.Settings.EquivalentTo(this.Settings)))
                {
                    throw new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported);
                }
            }
#endif

            try
            {
                if (settings.Pooling)
                {
                    MySqlPool pool = MySqlPoolManager.GetPool(settings);
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = pool.GetConnection();
                    }
                    procedureCache = pool.ProcedureCache;
                }
                else
                {
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = Driver.Create(settings);
                    }
                    procedureCache = new ProcedureCache((int)settings.ProcedureCacheSize);
                }
            }
            catch (Exception)
            {
                SetState(ConnectionState.Closed, true);
                throw;
            }

            SetState(ConnectionState.Open, false);
            driver.Configure(this);
            if (settings.Database != null && settings.Database != String.Empty)
            {
                ChangeDatabase(settings.Database);
            }

            // setup our schema provider
            schemaProvider = new ISSchemaProvider(this);
#if !CF
            perfMonitor = new PerformanceMonitor(this);
#endif

            // if we are opening up inside a current transaction, then autoenlist
            // TODO: control this with a connection string option
#if !MONO && !CF
            if (Transaction.Current != null && settings.AutoEnlist)
            {
                EnlistTransaction(Transaction.Current);
            }
#endif

            hasBeenOpen = true;
            SetState(ConnectionState.Open, true);
        }