public ConnectionImpl()

in src/main/user-impl/java/com/mysql/cj/jdbc/ConnectionImpl.java [337:455]


    public ConnectionImpl(HostInfo hostInfo) throws SQLException {
        try {
            // Stash away for later, used to clone this connection for Statement.cancel and Statement.setQueryTimeout().
            this.origHostInfo = hostInfo;
            this.origHostToConnectTo = hostInfo.getHost();
            this.origPortToConnectTo = hostInfo.getPort();

            this.database = hostInfo.getDatabase();
            this.user = hostInfo.getUser();
            this.password = hostInfo.getPassword();

            this.props = hostInfo.exposeAsProperties();
            this.propertySet = new JdbcPropertySetImpl();
            this.propertySet.initializeProperties(this.props);

            // We need Session ASAP to get access to central driver functionality
            this.nullStatementResultSetFactory = new ResultSetFactory(this, null);
            this.session = new NativeSession(hostInfo, this.propertySet);
            this.session.addListener(this); // listen for session status changes
        } catch (CJException e) {
            throw SQLExceptionsMapping.translateException(e, getExceptionInterceptor());
        }

        this.connectionSpan = this.session.getTelemetryHandler().startSpan(TelemetrySpanName.CONNECTION_CREATE);
        this.session.getTelemetryHandler().addLinkTarget(this.connectionSpan);
        try (TelemetryScope scope = this.connectionSpan.makeCurrent()) {
            this.connectionSpan.setAttribute(TelemetryAttribute.DB_CONNECTION_STRING, this::getURL);
            this.connectionSpan.setAttribute(TelemetryAttribute.DB_SYSTEM, TelemetryAttribute.DB_SYSTEM_DEFAULT);
            this.connectionSpan.setAttribute(TelemetryAttribute.DB_USER, this::getUser);
            this.connectionSpan.setAttribute(TelemetryAttribute.SERVER_ADDRESS, this.origHostToConnectTo);
            this.connectionSpan.setAttribute(TelemetryAttribute.SERVER_PORT, this.origPortToConnectTo);
            this.connectionSpan.setAttribute(TelemetryAttribute.THREAD_ID, () -> Thread.currentThread().getId());
            this.connectionSpan.setAttribute(TelemetryAttribute.THREAD_NAME, () -> Thread.currentThread().getName());

            try {
                String exceptionInterceptorClasses = this.propertySet.getStringProperty(PropertyKey.exceptionInterceptors).getStringValue();
                if (exceptionInterceptorClasses != null && !"".equals(exceptionInterceptorClasses)) {
                    this.exceptionInterceptor = new ExceptionInterceptorChain(exceptionInterceptorClasses, this.props, this.session.getLog());
                }

                // we can't cache fixed values here because properties are still not initialized with user provided values
                this.autoReconnectForPools = this.propertySet.getBooleanProperty(PropertyKey.autoReconnectForPools);
                this.cachePrepStmts = this.propertySet.getBooleanProperty(PropertyKey.cachePrepStmts);
                this.autoReconnect = this.propertySet.getBooleanProperty(PropertyKey.autoReconnect);
                this.useUsageAdvisor = this.propertySet.getBooleanProperty(PropertyKey.useUsageAdvisor);
                this.reconnectAtTxEnd = this.propertySet.getBooleanProperty(PropertyKey.reconnectAtTxEnd);
                this.emulateUnsupportedPstmts = this.propertySet.getBooleanProperty(PropertyKey.emulateUnsupportedPstmts);
                this.ignoreNonTxTables = this.propertySet.getBooleanProperty(PropertyKey.ignoreNonTxTables);
                this.pedantic = this.propertySet.getBooleanProperty(PropertyKey.pedantic);
                this.prepStmtCacheSqlLimit = this.propertySet.getIntegerProperty(PropertyKey.prepStmtCacheSqlLimit);
                this.useLocalSessionState = this.propertySet.getBooleanProperty(PropertyKey.useLocalSessionState);
                this.useServerPrepStmts = this.propertySet.getBooleanProperty(PropertyKey.useServerPrepStmts);
                this.processEscapeCodesForPrepStmts = this.propertySet.getBooleanProperty(PropertyKey.processEscapeCodesForPrepStmts);
                this.useLocalTransactionState = this.propertySet.getBooleanProperty(PropertyKey.useLocalTransactionState);
                this.disconnectOnExpiredPasswords = this.propertySet.getBooleanProperty(PropertyKey.disconnectOnExpiredPasswords);
                this.readOnlyPropagatesToServer = this.propertySet.getBooleanProperty(PropertyKey.readOnlyPropagatesToServer);

                if (this.cachePrepStmts.getValue()) {
                    createPreparedStatementCaches();
                }
                if (this.propertySet.getBooleanProperty(PropertyKey.cacheCallableStmts).getValue()) {
                    this.parsedCallableStatementCache = new LRUCache<>(this.propertySet.getIntegerProperty(PropertyKey.callableStmtCacheSize).getValue());
                }
                if (this.propertySet.getBooleanProperty(PropertyKey.allowMultiQueries).getValue()) {
                    this.propertySet.getProperty(PropertyKey.cacheResultSetMetadata).setValue(false); // we don't handle this yet
                }
                if (this.propertySet.getBooleanProperty(PropertyKey.cacheResultSetMetadata).getValue()) {
                    this.resultSetMetadataCache = new LRUCache<>(this.propertySet.getIntegerProperty(PropertyKey.metadataCacheSize).getValue());
                }
                if (this.propertySet.getStringProperty(PropertyKey.socksProxyHost).getStringValue() != null) {
                    this.propertySet.getProperty(PropertyKey.socketFactory).setValue(SocksProxySocketFactory.class.getName());
                }

                initializeSafeQueryInterceptors();
            } catch (CJException e) {
                throw SQLExceptionsMapping.translateException(e, getExceptionInterceptor());
            }

            try {
                createNewIO(false);

                unSafeQueryInterceptors();

                AbandonedConnectionCleanupThread.trackConnection(this, getSession().getNetworkResources());

                SocketAddress socketAddress = this.session.getRemoteSocketAddress();
                if (InetSocketAddress.class.isInstance(socketAddress)) {
                    InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
                    this.connectionSpan.setAttribute(TelemetryAttribute.NETWORK_PEER_ADDRESS, inetSocketAddress::getHostName);
                    this.connectionSpan.setAttribute(TelemetryAttribute.NETWORK_PEER_PORT, inetSocketAddress.getPort());
                    this.connectionSpan.setAttribute(TelemetryAttribute.NETWORK_TRANSPORT, TelemetryAttribute.NETWORK_TRANSPORT_TCP);
                }
                if (this.propertySet.getStringProperty(PropertyKey.socketFactory).getValue().equalsIgnoreCase("com.mysql.cj.protocol.NamedPipeSocketFactory")) {
                    this.connectionSpan.setAttribute(TelemetryAttribute.NETWORK_TRANSPORT, TelemetryAttribute.NETWORK_TRANSPORT_PIPE);
                } else if (StringUtils.indexOfIgnoreCase(socketAddress.getClass().getName(), "UNIXSocket") >= 0) {
                    this.connectionSpan.setAttribute(TelemetryAttribute.NETWORK_TRANSPORT, TelemetryAttribute.NETWORK_TRANSPORT_UNIX);
                }

            } catch (SQLException ex) {
                cleanup(ex);

                // don't clobber SQL exceptions
                throw ex;
            } catch (Exception ex) {
                cleanup(ex);

                throw SQLError.createSQLException(
                        this.propertySet.getBooleanProperty(PropertyKey.paranoid).getValue() ? Messages.getString("Connection.0")
                                : Messages.getString("Connection.1",
                                        new Object[] { this.session.getHostInfo().getHost(), this.session.getHostInfo().getPort() }),
                        MysqlErrorNumbers.SQLSTATE_MYSQL_COMMUNICATION_LINK_FAILURE, ex, getExceptionInterceptor());
            }
        } catch (Throwable t) {
            this.connectionSpan.setError(t);
            throw t;
        } finally {
            this.connectionSpan.end();
        }
    }