Example #1
0
        protected override void Open()
        {
            if (Settings.ConnectionProtocol == MySqlConnectionProtocol.Tcp && Settings.IsSshEnabled())
            {
                _sshClient = MySqlSshClientManager.SetupSshClient(
                    Settings.SshHostName,
                    Settings.SshUserName,
                    Settings.SshPassword,
                    Settings.SshKeyFile,
                    Settings.SshPassphrase,
                    Settings.SshPort,
                    Settings.Server,
                    Settings.Port,
                    true);
            }

            bool isUnix = Settings.ConnectionProtocol == MySqlConnectionProtocol.Unix ||
                          Settings.ConnectionProtocol == MySqlConnectionProtocol.UnixSocket;

            _stream = MyNetworkStream.CreateStream(
                Settings.Server == "127.0.0.1" || Settings.Server == "::1"
            ? "localhost"
            : Settings.Server,
                Settings.ConnectTimeout,
                Settings.Keepalive,
                Settings.Port,
                isUnix);
            if (_stream == null)
            {
                throw new MySqlException(ResourcesX.UnableToConnect);
            }

            _reader  = new XPacketReaderWriter(_stream);
            _writer  = new XPacketReaderWriter(_stream);
            protocol = new XProtocol(_reader, _writer);

            Settings.CharacterSet = String.IsNullOrWhiteSpace(Settings.CharacterSet) ? "utf8mb4" : Settings.CharacterSet;

            var encoding = Encoding.GetEncoding(String.Compare(Settings.CharacterSet, "utf8mb4", true) == 0 ? "UTF-8" : Settings.CharacterSet);

            SetState(SessionState.Connecting, false);

            try
            {
                GetAndSetCapabilities();
            }
            catch (Exception)
            {
                if (_sshClient != null && _sshClient.IsConnected)
                {
                    _sshClient.Disconnect();
                }

                throw;
            }

            // Validates use of TLS.
            if (Settings.SslMode != MySqlSslMode.None)
            {
                if (serverSupportsTls)
                {
                    new Ssl(
                        Settings.Server,
                        Settings.SslMode,
                        Settings.CertificateFile,
                        Settings.CertificateStoreLocation,
                        Settings.CertificatePassword,
                        Settings.CertificateThumbprint,
                        Settings.SslCa,
                        Settings.SslCert,
                        Settings.SslKey)
                    .StartSSL(ref _stream, encoding, Settings.ToString());
                    _reader = new XPacketReaderWriter(_stream);
                    _writer = new XPacketReaderWriter(_stream);
                    protocol.SetXPackets(_reader, _writer);
                }
                else
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }

            Authenticate();

            SetState(SessionState.Open, false);
        }
Example #2
0
        /// <include file='docs/MySqlConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                Throw(new InvalidOperationException(Resources.ConnectionAlreadyOpen));
            }

            // start up our interceptors
            _exceptionInterceptor = new ExceptionInterceptor(this);
            commandInterceptor    = new CommandInterceptor(this);

            SetState(ConnectionState.Connecting, true);

#if !NETSTANDARD1_6
            AssertPermissions();

            //TODO: SUPPORT FOR 452 AND 46X
            // 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 (Settings.ConnectionProtocol == MySqlConnectionProtocol.Tcp && Settings.IsSshEnabled())
                {
                    _sshClient = MySqlSshClientManager.SetupSshClient(
                        Settings.SshHostName,
                        Settings.SshUserName,
                        Settings.SshPassword,
                        Settings.SshKeyFile,
                        Settings.SshPassphrase,
                        Settings.SshPort,
                        Settings.Server,
                        Settings.Port,
                        false);
                }

                //ÊÇ·ñΪCluster»·¾³
                bool isCluster = false;
                //TODO: SUPPORT FOR 452 AND 46X
                // Load balancing
#if !NETSTANDARD1_6
                if (ReplicationManager.IsReplicationGroup(Settings.Server))
                {
                    isCluster = true;
                    if (driver == null)
                    {
                        ReplicationManager.GetNewConnection(Settings.Server, false, this);
                    }
                    else
                    {
                        currentSettings = driver.Settings;
                    }
                }
#endif

                if (Settings.Pooling)
                {
                    if (isCluster)
                    {
                        if (driver != null)
                        {
                            ProcedureCache = driver.Pool.ProcedureCache;
                        }
                        else
                        {
                            Console.WriteLine("cluster driver error >> driver == null >> " + currentSettings.ConnectionString);
                        }
                    }
                    else
                    {
                        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)
            {
                SetState(ConnectionState.Closed, true);
                throw;
            }

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

            if (!(driver.SupportsPasswordExpiration && driver.IsPasswordExpired))
            {
                if (!string.IsNullOrEmpty(Settings.Database))
                {
                    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);
        }