Пример #1
0
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
                Throw(new InvalidOperationException(Resources.ConnectionAlreadyOpen));

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

            SetState(ConnectionState.Connecting, true);

            AssertPermissions();

#if !NETSTANDARD1_6
            // 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
            {
                MySqlConnectionStringBuilder currentSettings = Settings;
#if SUPPORT_REPLICATION

        // Load balancing 
        if (ReplicationManager.IsReplicationGroup(Settings.Server))
        {
          if (driver == null)
          {
            ReplicationManager.GetNewConnection(Settings.Server, false, this);
          }
          else
            currentSettings = driver.Settings;
        }
#endif
#if !NETSTANDARD1_6
                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);
                }
#else
                if (driver == null || !driver.IsOpen)
                        driver = Driver.Create(currentSettings);
                    procedureCache = new ProcedureCache((int)Settings.ProcedureCacheSize);
#endif
            }
            catch (Exception ex)
            {
                SetState(ConnectionState.Closed, true);
                throw ex;
            }

            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
            // TODO: control this with a connection string option
#if !NETSTANDARD1_6
            if (Transaction.Current != null && Settings.AutoEnlist)
                EnlistTransaction(Transaction.Current);
#endif

            hasBeenOpen = true;
            SetState(ConnectionState.Open, true);
        }
Пример #2
0
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                Throw(new InvalidOperationException(Resources.ConnectionAlreadyOpen));
            }

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

            SetState(ConnectionState.Connecting, true);

            AssertPermissions();

#if !NETSTANDARD1_6
            // 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
            {
                MySqlConnectionStringBuilder currentSettings = Settings;
#if SUPPORT_REPLICATION
                // Load balancing
                if (ReplicationManager.IsReplicationGroup(Settings.Server))
                {
                    if (driver == null)
                    {
                        ReplicationManager.GetNewConnection(Settings.Server, false, this);
                    }
                    else
                    {
                        currentSettings = driver.Settings;
                    }
                }
#endif
#if !NETSTANDARD1_6
                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);
                }
#else
                if (driver == null || !driver.IsOpen)
                {
                    driver = Driver.Create(currentSettings);
                }
                procedureCache = new ProcedureCache((int)Settings.ProcedureCacheSize);
#endif
            }
            catch (Exception ex)
            {
                SetState(ConnectionState.Closed, true);
                throw ex;
            }

            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
            // TODO: control this with a connection string option
#if !NETSTANDARD1_6
            if (Transaction.Current != null && Settings.AutoEnlist)
            {
                EnlistTransaction(Transaction.Current);
            }
#endif

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