示例#1
0
文件: Driver.cs 项目: tvrjcf/Demo
        protected virtual void Dispose(bool disposing)
        {
            // if we are pooling, then release ourselves
            if (connectionString.Pooling)
            {
                MySqlPoolManager.RemoveConnection(this);
            }

            isOpen = false;
        }
示例#2
0
 /// <summary>
 /// Aborts 当前实例。
 /// </summary>
 internal void Abort()
 {
     try
     {
         if (settings.Pooling)
         {
             MySqlPoolManager.ReleaseConnection(driver);
         }
         else
         {
             driver.Close();
         }
     }
     catch (Exception)
     {
     }
     SetState(ConnectionState.Closed, true);
 }
示例#3
0
        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;
        }
示例#4
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);
            }
        }
示例#5
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            // Avoid cyclic calls to Dispose.
            disposed = true;
            try
            {
                ResetTimeout(1000);
                handler.Close(isOpen);
                // if we are pooling, then release ourselves
                if (connectionString.Pooling)
                {
                    MySqlPoolManager.RemoveConnection(this);
                }
            }
            catch (Exception ex)
            {
                if (disposing)
                {
                    MySqlException mysqlEx = ex as MySqlException;
                    if (mysqlEx == null)
                    {
                        MySqlTrace.LogError(0, ex.GetBaseException().Message);
                    }
                    else
                    {
                        MySqlTrace.LogError(mysqlEx.Number, ex.GetBaseException().Message);
                    }
                }
            }
            finally
            {
                reader = null;
                isOpen = false;
            }
        }
示例#6
0
 private static void EnsureClearingPools(object sender, EventArgs e)
 {
     MySqlPoolManager.ClearAllPools();
 }
示例#7
0
 /// <include file='docs/MySqlConnection.xml' path='docs/ClearAllPools/*'/>
 public static void ClearAllPools()
 {
     MySqlPoolManager.ClearAllPools();
 }
示例#8
0
 /// <include file='docs/MySqlConnection.xml' path='docs/ClearPool/*'/>
 public static void ClearPool(MySqlConnection connection)
 {
     MySqlPoolManager.ClearPool(connection.Settings);
 }
示例#9
0
        /// <summary>
        /// Открывает подключение к базе данных со значениями свойств, определяемыми объектом <see cref="MySqlConnection.ConnectionString"/>.
        /// </summary>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                Throw(new InvalidOperationException(MySqlResources.ConnectionAlreadyOpen));
            }

#if !RT
            // start up our interceptors
            exceptionInterceptor = new ExceptionInterceptor(this);
            commandInterceptor   = new CommandInterceptor(this);
#endif

            SetState(ConnectionState.Connecting, true);

            AssertPermissions();

#if !RT
            // 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(MySqlResources.MultipleConnectionsInTransactionNotSupported));
                }
            }
#endif

            try
            {
                MySqlConnectionStringBuilder currentSettings = Settings;

                // Load balancing
                if (ReplicationManager.IsReplicationGroup(Settings.Server))
                {
                    if (driver == null)
                    {
                        ReplicationManager.GetNewConnection(Settings.Server, false, this);
                    }
                    else
                    {
                        currentSettings = driver.Settings;
                    }
                }

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

            // if the user is using old syntax, let them know
            if (driver.Settings.UseOldSyntax)
            {
                MySqlTrace.LogWarning(ServerThread,
                                      "You are using old syntax that will be removed in future versions");
            }

            SetState(ConnectionState.Open, false);
            driver.Configure(this);

            if (!(driver.SupportsPasswordExpiration && driver.IsPasswordExpired))
            {
                if (Settings.Database != null && Settings.Database != String.Empty)
                {
                    ChangeDatabase(Settings.Database);
                }
            }

            // setup our schema provider
            schemaProvider = new ISSchemaProvider(this);
            perfMonitor    = new PerformanceMonitor(this);

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

            hasBeenOpen = true;
            SetState(ConnectionState.Open, true);
        }
示例#10
0
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                throw new InvalidOperationException(System.Data.MySqlClient.Properties.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(System.Data.MySqlClient.Properties.Resources.MultipleConnectionsInTransactionNotSupported);
                }
            }
#endif

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

            // if the user is using old syntax, let them know
            if (driver.Settings.UseOldSyntax)
            {
                Logger.LogWarning("You are using old syntax that will be removed in future versions");
            }

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

            // setup our schema provider
            if (driver.Version.isAtLeast(5, 0, 0))
            {
                schemaProvider = new ISSchemaProvider(this);
            }
            else
            {
                schemaProvider = new SchemaProvider(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);
        }