Java源码示例:com.mysql.cj.exceptions.UnableToConnectException

示例1
protected SocketFactory createSocketFactory(String socketFactoryClassName) {
    try {
        if (socketFactoryClassName == null) {
            throw ExceptionFactory.createException(UnableToConnectException.class, Messages.getString("SocketConnection.0"), getExceptionInterceptor());
        }

        Object sf = Class.forName(socketFactoryClassName).newInstance();
        if (sf instanceof SocketFactory) {
            return (SocketFactory) (Class.forName(socketFactoryClassName).newInstance());
        }

        // wrap legacy socket factories
        return new SocketFactoryWrapper(sf);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("SocketConnection.1", new String[] { socketFactoryClassName }), getExceptionInterceptor());
    }
}
 
示例2
/**
 * Creates an IO channel to the server
 * 
 * @param isForReconnect
 *            is this request for a re-connect
 * @throws CommunicationsException
 */
public void createNewIO(boolean isForReconnect) {
    synchronized (getConnectionMutex()) {
        // Synchronization Not needed for *new* connections, but defintely for connections going through fail-over, since we might get the new connection up
        // and running *enough* to start sending cached or still-open server-side prepared statements over to the backend before we get a chance to
        // re-prepare them...

        try {
            if (!this.autoReconnect.getValue()) {
                connectOneTryOnly(isForReconnect);

                return;
            }

            connectWithRetries(isForReconnect);
        } catch (SQLException ex) {
            throw ExceptionFactory.createException(UnableToConnectException.class, ex.getMessage(), ex);
        }
    }
}
 
示例3
@Override
public void createNewIO(boolean isForReconnect) {
    synchronized (getConnectionMutex()) {
        // Synchronization Not needed for *new* connections, but defintely for connections going through fail-over, since we might get the new connection up
        // and running *enough* to start sending cached or still-open server-side prepared statements over to the backend before we get a chance to
        // re-prepare them...

        try {
            if (!this.autoReconnect.getValue()) {
                connectOneTryOnly(isForReconnect);

                return;
            }

            connectWithRetries(isForReconnect);
        } catch (SQLException ex) {
            throw ExceptionFactory.createException(UnableToConnectException.class, ex.getMessage(), ex);
        }
    }
}
 
示例4
protected SocketFactory createSocketFactory(String socketFactoryClassName) {
    try {
        if (socketFactoryClassName == null) {
            throw ExceptionFactory.createException(UnableToConnectException.class, Messages.getString("SocketConnection.0"), getExceptionInterceptor());
        }

        Object sf = Class.forName(socketFactoryClassName).newInstance();
        if (sf instanceof SocketFactory) {
            return (SocketFactory) (Class.forName(socketFactoryClassName).newInstance());
        }

        // wrap legacy socket factories
        return new SocketFactoryWrapper(sf);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("SocketConnection.1", new String[] { socketFactoryClassName }), getExceptionInterceptor());
    }
}
 
示例5
@BuildStep
void registerExceptionsForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJCommunicationsException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, CJConnectionFeatureNotAvailableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJTimeoutException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJPacketTooBigException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, AssertionFailedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ClosedOnExpiredPasswordException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ConnectionIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataConversionException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataReadException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataTruncationException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DeadlockTimeoutRollbackMarker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, FeatureNotAvailableException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, InvalidConnectionAttributeException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NumberOutOfRange.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, OperationCancelledException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PasswordExpiredException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PropertyNotModifiableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, RSAException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, SSLParamsException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StatementIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StreamingNotifiable.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, UnableToConnectException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, UnsupportedConnectionStringException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, WrongArgumentException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "com.mysql.cj.jdbc.MysqlXAException"));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, StandardLoadBalanceExceptionChecker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NdbLoadBalanceExceptionChecker.class.getName()));
}
 
示例6
/**
 * Try to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given
 * URL. This will be common, as when the JDBC driverManager is asked to connect to a given URL, it passes the URL to each loaded driver in turn.
 * 
 * <p>
 * The driver should raise an SQLException if the URL is null or if it is the right driver to connect to the given URL, but has trouble connecting to the
 * database.
 * </p>
 * 
 * <p>
 * The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments. These properties take precedence over any
 * properties sent in the URL.
 * </p>
 * 
 * <p>
 * MySQL protocol takes the form:
 * 
 * <PRE>
 * jdbc:mysql://host:port/database
 * </PRE>
 * 
 * </p>
 * 
 * @param url
 *            the URL of the database to connect to
 * @param info
 *            a list of arbitrary tag/value pairs as connection arguments
 * 
 * @return a connection to the URL or null if it isn't us
 * 
 * @exception SQLException
 *                if a database access error occurs or the url is {@code null}
 * 
 * @see java.sql.Driver#connect
 */
public java.sql.Connection connect(String url, Properties info) throws SQLException {

    try {
        ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (conStr.getType() == null) {
            /*
             * According to JDBC spec:
             * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the
             * JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.
             */
            return null;
        }

        switch (conStr.getType()) {
            case LOADBALANCE_CONNECTION:
                return LoadBalancedConnectionProxy.createProxyInstance((LoadbalanceConnectionUrl) conStr);

            case FAILOVER_CONNECTION:
                return FailoverConnectionProxy.createProxyInstance(conStr);

            case REPLICATION_CONNECTION:
                return ReplicationConnectionProxy.createProxyInstance((ReplicationConnectionUrl) conStr);

            case XDEVAPI_SESSION:
                // TODO test it
                //return new XJdbcConnection(conStr.getProperties());

            default:
                return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost());

        }

    } catch (CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("NonRegisteringDriver.17", new Object[] { ex.toString() }), ex);
    }
}
 
示例7
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
示例8
private void connectOneTryOnly(boolean isForReconnect) throws SQLException {
    Exception connectionNotEstablishedBecause = null;

    try {

        JdbcConnection c = getProxy();
        this.session.connect(this.origHostInfo, this.user, this.password, this.database, DriverManager.getLoginTimeout() * 1000, c);

        // save state from old connection
        boolean oldAutoCommit = getAutoCommit();
        int oldIsolationLevel = this.isolationLevel;
        boolean oldReadOnly = isReadOnly(false);
        String oldCatalog = getCatalog();

        this.session.setQueryInterceptors(this.queryInterceptors);

        // Server properties might be different from previous connection, so initialize again...
        initializePropsFromServer();

        if (isForReconnect) {
            // Restore state from old connection
            setAutoCommit(oldAutoCommit);
            setTransactionIsolation(oldIsolationLevel);
            setCatalog(oldCatalog);
            setReadOnly(oldReadOnly);
        }
        return;

    } catch (UnableToConnectException rejEx) {
        close();
        this.session.getProtocol().getSocketConnection().forceClose();
        throw rejEx;

    } catch (Exception EEE) {

        if ((EEE instanceof PasswordExpiredException
                || EEE instanceof SQLException && ((SQLException) EEE).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD)
                && !this.disconnectOnExpiredPasswords.getValue()) {
            return;
        }

        if (this.session != null) {
            this.session.forceClose();
        }

        connectionNotEstablishedBecause = EEE;

        if (EEE instanceof SQLException) {
            throw (SQLException) EEE;
        }

        if (EEE.getCause() != null && EEE.getCause() instanceof SQLException) {
            throw (SQLException) EEE.getCause();
        }

        if (EEE instanceof CJException) {
            throw (CJException) EEE;
        }

        SQLException chainedEx = SQLError.createSQLException(Messages.getString("Connection.UnableToConnect"),
                MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());
        chainedEx.initCause(connectionNotEstablishedBecause);

        throw chainedEx;
    }
}
 
示例9
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
示例10
private void connectOneTryOnly(boolean isForReconnect) throws SQLException {
    Exception connectionNotEstablishedBecause = null;

    try {

        JdbcConnection c = getProxy();
        this.session.connect(this.origHostInfo, this.user, this.password, this.database, DriverManager.getLoginTimeout() * 1000, c);

        // save state from old connection
        boolean oldAutoCommit = getAutoCommit();
        int oldIsolationLevel = this.isolationLevel;
        boolean oldReadOnly = isReadOnly(false);
        String oldCatalog = getCatalog();

        this.session.setQueryInterceptors(this.queryInterceptors);

        // Server properties might be different from previous connection, so initialize again...
        initializePropsFromServer();

        if (isForReconnect) {
            // Restore state from old connection
            setAutoCommit(oldAutoCommit);
            setTransactionIsolation(oldIsolationLevel);
            setCatalog(oldCatalog);
            setReadOnly(oldReadOnly);
        }
        return;

    } catch (UnableToConnectException rejEx) {
        close();
        this.session.getProtocol().getSocketConnection().forceClose();
        throw rejEx;

    } catch (Exception EEE) {

        if ((EEE instanceof PasswordExpiredException
                || EEE instanceof SQLException && ((SQLException) EEE).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD)
                && !this.disconnectOnExpiredPasswords.getValue()) {
            return;
        }

        if (this.session != null) {
            this.session.forceClose();
        }

        connectionNotEstablishedBecause = EEE;

        if (EEE instanceof SQLException) {
            throw (SQLException) EEE;
        }

        if (EEE.getCause() != null && EEE.getCause() instanceof SQLException) {
            throw (SQLException) EEE.getCause();
        }

        if (EEE instanceof CJException) {
            throw (CJException) EEE;
        }

        SQLException chainedEx = SQLError.createSQLException(Messages.getString("Connection.UnableToConnect"),
                MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());
        chainedEx.initCause(connectionNotEstablishedBecause);

        throw chainedEx;
    }
}
 
示例11
/**
 * Try to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given
 * URL. This will be common, as when the JDBC driverManager is asked to connect to a given URL, it passes the URL to each loaded driver in turn.
 * 
 * <p>
 * The driver should raise an SQLException if the URL is null or if it is the right driver to connect to the given URL, but has trouble connecting to the
 * database.
 * </p>
 * 
 * <p>
 * The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments. These properties take precedence over any
 * properties sent in the URL.
 * </p>
 * 
 * <p>
 * MySQL protocol takes the form: jdbc:mysql://host:port/database
 * </p>
 * 
 * @param url
 *            the URL of the database to connect to
 * @param info
 *            a list of arbitrary tag/value pairs as connection arguments
 * 
 * @return a connection to the URL or null if it isn't us
 * 
 * @exception SQLException
 *                if a database access error occurs or the url is {@code null}
 */
@Override
public java.sql.Connection connect(String url, Properties info) throws SQLException {

    try {
        if (!ConnectionUrl.acceptsUrl(url)) {
            /*
             * According to JDBC spec:
             * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the
             * JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.
             */
            return null;
        }

        ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        switch (conStr.getType()) {
            case SINGLE_CONNECTION:
                return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost());

            case LOADBALANCE_CONNECTION:
                return LoadBalancedConnectionProxy.createProxyInstance((LoadbalanceConnectionUrl) conStr);

            case FAILOVER_CONNECTION:
                return FailoverConnectionProxy.createProxyInstance(conStr);

            case REPLICATION_CONNECTION:
                return ReplicationConnectionProxy.createProxyInstance((ReplicationConnectionUrl) conStr);

            default:
                return null;
        }

    } catch (UnsupportedConnectionStringException e) {
        // when Connector/J can't handle this connection string the Driver must return null
        return null;

    } catch (CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("NonRegisteringDriver.17", new Object[] { ex.toString() }), ex);
    }
}