Java Code Examples for java.sql.SQLException.initCause()

The following are Jave code examples for showing how to use initCause() of the java.sql.SQLException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: SerialExceptionTests.java   View Source Code Vote up 6 votes
@Test
public void test04() {
    SQLException ex = new SerialException("Exception 1");
    ex.initCause(t1);
    SerialException ex1 = new SerialException("Exception 2");
    SerialException ex2 = new SerialException("Exception 3");
    ex2.initCause(t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    while (ex != null) {
        assertTrue(msgs[num++].equals(ex.getMessage()));
        Throwable c = ex.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        ex = ex.getNextException();
    }
}
 
Example 2
Project: jdk8u-jdk   File: SyncFactoryExceptionTests.java   View Source Code Vote up 6 votes
@Test
public void test04() {
    SQLException ex = new SyncFactoryException("Exception 1");
    ex.initCause(t1);
    SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
    SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
    ex2.initCause(t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    while (ex != null) {
        assertTrue(msgs[num++].equals(ex.getMessage()));
        Throwable c = ex.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        ex = ex.getNextException();
    }
}
 
Example 3
Project: BibliotecaPS   File: MysqlIO.java   View Source Code Vote up 6 votes
private SocketFactory createSocketFactory() throws SQLException {
    try {
        if (this.socketFactoryClassName == null) {
            throw SQLError.createSQLException(Messages.getString("MysqlIO.75"), SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE,
                    getExceptionInterceptor());
        }

        return (SocketFactory) (Class.forName(this.socketFactoryClassName).newInstance());
    } catch (Exception ex) {
        SQLException sqlEx = SQLError.createSQLException(Messages.getString("MysqlIO.76") + this.socketFactoryClassName + Messages.getString("MysqlIO.77"),
                SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());

        sqlEx.initCause(ex);

        throw sqlEx;
    }
}
 
Example 4
Project: openjdk-jdk10   File: SyncFactoryExceptionTests.java   View Source Code Vote up 6 votes
@Test
public void test04() {
    SQLException ex = new SyncFactoryException("Exception 1");
    ex.initCause(t1);
    SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
    SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
    ex2.initCause(t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    while (ex != null) {
        assertTrue(msgs[num++].equals(ex.getMessage()));
        Throwable c = ex.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        ex = ex.getNextException();
    }
}
 
Example 5
Project: OpenVertretung   File: JDBC4MysqlSQLXML.java   View Source Code Vote up 6 votes
protected String domSourceToString() throws SQLException {
    try {
        DOMSource source = new DOMSource(this.asDOMResult.getNode());
        Transformer identity = TransformerFactory.newInstance().newTransformer();
        StringWriter stringOut = new StringWriter();
        Result result = new StreamResult(stringOut);
        identity.transform(source, result);

        return stringOut.toString();
    } catch (Throwable t) {
        SQLException sqlEx = SQLError.createSQLException(t.getMessage(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        sqlEx.initCause(t);

        throw sqlEx;
    }
}
 
Example 6
Project: lams   File: JDBC4MysqlSQLXML.java   View Source Code Vote up 6 votes
protected String domSourceToString() throws SQLException {
    try {
        DOMSource source = new DOMSource(this.asDOMResult.getNode());
        Transformer identity = TransformerFactory.newInstance().newTransformer();
        StringWriter stringOut = new StringWriter();
        Result result = new StreamResult(stringOut);
        identity.transform(source, result);

        return stringOut.toString();
    } catch (Throwable t) {
        SQLException sqlEx = SQLError.createSQLException(t.getMessage(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        sqlEx.initCause(t);

        throw sqlEx;
    }
}
 
Example 7
Project: the-vigilantes   File: ResultSetScannerInterceptor.java   View Source Code Vote up 6 votes
public void init(Connection conn, Properties props) throws SQLException {
    String regexFromUser = props.getProperty("resultSetScannerRegex");

    if (regexFromUser == null || regexFromUser.length() == 0) {
        throw new SQLException("resultSetScannerRegex must be configured, and must be > 0 characters");
    }

    try {
        this.regexP = Pattern.compile(regexFromUser);
    } catch (Throwable t) {
        SQLException sqlEx = new SQLException("Can't use configured regex due to underlying exception.");
        sqlEx.initCause(t);

        throw sqlEx;
    }

}
 
Example 8
Project: ProyectoPacientes   File: ResultSetMetaData.java   View Source Code Vote up 6 votes
/**
 * What's the Java character encoding name for the given column?
 * 
 * @param column
 *            the first column is 1, the second is 2, etc.
 * 
 * @return the Java character encoding name for the given column, or null if
 *         no Java character encoding maps to the MySQL character set for
 *         the given column.
 * 
 * @throws SQLException
 *             if an invalid column index is given.
 */
public String getColumnCharacterEncoding(int column) throws SQLException {
    String mysqlName = getColumnCharacterSet(column);

    String javaName = null;

    if (mysqlName != null) {
        try {
            javaName = CharsetMapping.getJavaEncodingForMysqlCharset(mysqlName);
        } catch (RuntimeException ex) {
            SQLException sqlEx = SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, null);
            sqlEx.initCause(ex);
            throw sqlEx;
        }
    }

    return javaName;
}
 
Example 9
Project: ProyectoPacientes   File: MysqlIO.java   View Source Code Vote up 6 votes
private SocketFactory createSocketFactory() throws SQLException {
    try {
        if (this.socketFactoryClassName == null) {
            throw SQLError.createSQLException(Messages.getString("MysqlIO.75"), SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE,
                    getExceptionInterceptor());
        }

        return (SocketFactory) (Class.forName(this.socketFactoryClassName).newInstance());
    } catch (Exception ex) {
        SQLException sqlEx = SQLError.createSQLException(Messages.getString("MysqlIO.76") + this.socketFactoryClassName + Messages.getString("MysqlIO.77"),
                SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());

        sqlEx.initCause(ex);

        throw sqlEx;
    }
}
 
Example 10
Project: ProyectoPacientes   File: Blob.java   View Source Code Vote up 6 votes
/**
 * @see Blob#setBytes(long, byte[], int, int)
 */
public synchronized int setBytes(long writeAt, byte[] bytes, int offset, int length) throws SQLException {
    checkClosed();

    OutputStream bytesOut = setBinaryStream(writeAt);

    try {
        bytesOut.write(bytes, offset, length);
    } catch (IOException ioEx) {
        SQLException sqlEx = SQLError.createSQLException(Messages.getString("Blob.1"), SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
        sqlEx.initCause(ioEx);

        throw sqlEx;
    } finally {
        try {
            bytesOut.close();
        } catch (IOException doNothing) {
            // do nothing
        }
    }

    return length;
}
 
Example 11
Project: the-vigilantes   File: MysqlIO.java   View Source Code Vote up 6 votes
private SocketFactory createSocketFactory() throws SQLException {
    try {
        if (this.socketFactoryClassName == null) {
            throw SQLError.createSQLException(Messages.getString("MysqlIO.75"), SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE,
                    getExceptionInterceptor());
        }

        return (SocketFactory) (Class.forName(this.socketFactoryClassName).newInstance());
    } catch (Exception ex) {
        SQLException sqlEx = SQLError.createSQLException(Messages.getString("MysqlIO.76") + this.socketFactoryClassName + Messages.getString("MysqlIO.77"),
                SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());

        sqlEx.initCause(ex);

        throw sqlEx;
    }
}
 
Example 12
Project: OpenVertretung   File: ResultSetScannerInterceptor.java   View Source Code Vote up 6 votes
public void init(Connection conn, Properties props) throws SQLException {
    String regexFromUser = props.getProperty("resultSetScannerRegex");

    if (regexFromUser == null || regexFromUser.length() == 0) {
        throw new SQLException("resultSetScannerRegex must be configured, and must be > 0 characters");
    }

    try {
        this.regexP = Pattern.compile(regexFromUser);
    } catch (Throwable t) {
        SQLException sqlEx = new SQLException("Can't use configured regex due to underlying exception.");
        sqlEx.initCause(t);

        throw sqlEx;
    }

}
 
Example 13
Project: the-vigilantes   File: SQLError.java   View Source Code Vote up 5 votes
public static SQLException createSQLException(String message, String sqlState, Throwable cause, ExceptionInterceptor interceptor, Connection conn) {
    SQLException sqlEx = createSQLException(message, sqlState, null);
    if (sqlEx.getCause() == null) {
        sqlEx.initCause(cause);
    }
    // Run through the exception interceptor after setting the init cause.
    return runThroughExceptionInterceptor(interceptor, sqlEx, conn);
}
 
Example 14
Project: lams   File: ConnectionImpl.java   View Source Code Vote up 5 votes
public void throwConnectionClosedException() throws SQLException {
    SQLException ex = SQLError.createSQLException("No operations allowed after connection closed.", SQLError.SQL_STATE_CONNECTION_NOT_OPEN,
            getExceptionInterceptor());

    if (this.forceClosedReason != null) {
        ex.initCause(this.forceClosedReason);
    }

    throw ex;
}
 
Example 15
Project: lams   File: ConnectionImpl.java   View Source Code Vote up 4 votes
private void connectOneTryOnly(boolean isForReconnect, Properties mergedProps) throws SQLException {
    Exception connectionNotEstablishedBecause = null;

    try {

        coreConnect(mergedProps);
        this.connectionId = this.io.getThreadId();
        this.isClosed = false;

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

        this.io.setStatementInterceptors(this.statementInterceptors);

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

        if (isForReconnect) {
            // Restore state from old connection
            setAutoCommit(oldAutoCommit);

            if (this.hasIsolationLevels) {
                setTransactionIsolation(oldIsolationLevel);
            }

            setCatalog(oldCatalog);

            setReadOnly(oldReadOnly);
        }
        return;

    } catch (Exception EEE) {

        if (EEE instanceof SQLException && ((SQLException) EEE).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD
                && !getDisconnectOnExpiredPasswords()) {
            return;
        }

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

        connectionNotEstablishedBecause = EEE;

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

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

        throw chainedEx;
    }
}
 
Example 16
Project: apache-tomcat-7.0.73-with-comment   File: ConnectionPool.java   View Source Code Vote up 4 votes
/**
 * Creates a JDBC connection and tries to connect to the database.
 * @param now timestamp of when this was called
 * @param notUsed Argument not used
 * @return a PooledConnection that has been connected
 * @throws SQLException
 */
protected PooledConnection createConnection(long now, PooledConnection notUsed, String username, String password) throws SQLException {
    //no connections where available we'll create one
    PooledConnection con = create(false);
    if (username!=null) con.getAttributes().put(PooledConnection.PROP_USER, username);
    if (password!=null) con.getAttributes().put(PooledConnection.PROP_PASSWORD, password);
    boolean error = false;
    try {
        //connect and validate the connection
        con.lock();
        con.connect();
        if (con.validate(PooledConnection.VALIDATE_INIT)) {
            //no need to lock a new one, its not contented
            con.setTimestamp(now);
            if (getPoolProperties().isLogAbandoned()) {
                con.setStackTrace(getThreadDump());
            }
            if (!busy.offer(con)) {
                log.debug("Connection doesn't fit into busy array, connection will not be traceable.");
            }
            return con;
        } else {
            //validation failed, make sure we disconnect
            //and clean up
            throw new SQLException("Validation Query Failed, enable logValidationErrors for more details.");
        } //end if
    } catch (Exception e) {
        error = true;
        if (log.isDebugEnabled())
            log.debug("Unable to create a new JDBC connection.", e);
        if (e instanceof SQLException) {
            throw (SQLException)e;
        } else {
            SQLException ex = new SQLException(e.getMessage());
            ex.initCause(e);
            throw ex;
        }
    } finally {
        // con can never be null here
        if (error ) {
            release(con);
        }
        con.unlock();
    }//catch
}
 
Example 17
Project: the-vigilantes   File: ConnectionImpl.java   View Source Code Vote up 4 votes
private void connectOneTryOnly(boolean isForReconnect, Properties mergedProps) throws SQLException {
    Exception connectionNotEstablishedBecause = null;

    try {

        coreConnect(mergedProps);
        this.connectionId = this.io.getThreadId();
        this.isClosed = false;

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

        this.io.setStatementInterceptors(this.statementInterceptors);

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

        if (isForReconnect) {
            // Restore state from old connection
            setAutoCommit(oldAutoCommit);

            if (this.hasIsolationLevels) {
                setTransactionIsolation(oldIsolationLevel);
            }

            setCatalog(oldCatalog);

            setReadOnly(oldReadOnly);
        }
        return;

    } catch (Exception EEE) {

        if (EEE instanceof SQLException && ((SQLException) EEE).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD
                && !getDisconnectOnExpiredPasswords()) {
            return;
        }

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

        connectionNotEstablishedBecause = EEE;

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

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

        throw chainedEx;
    }
}
 
Example 18
Project: sstore-soft   File: JDBCSQLXML.java   View Source Code Vote up 3 votes
/**
 * Retrieves a new SQLXML result instantiation exception.
 *
 * @param cause of the exception.
 * @return a new SQLXML result instantiation exception
 */
static SQLException resultInstantiation(Throwable cause) {

    SQLException ex = Util.sqlException(ErrorCode.GENERAL_ERROR,
        "SQLXML Result instantiation failed: " + cause);

    ex.initCause(cause);

    return ex;
}
 
Example 19
Project: s-store   File: JDBCSQLXML.java   View Source Code Vote up 3 votes
/**
 *  Retrieves a new SQLXML DOM instantiation exception.
 *
 * @param cause of the exception
 */
static SQLException domInstantiation(Throwable cause) {

    SQLException ex = Util.sqlException(ErrorCode.GENERAL_ERROR,
        "SQLXML DOM instantiation failed: " + cause);

    ex.initCause(cause);

    return ex;
}
 
Example 20
Project: s-store   File: JDBCSQLXML.java   View Source Code Vote up 3 votes
/**
 * Retrieves a new SQLXML result instantiation exception.
 *
 * @param cause of the exception.
 * @return a new SQLXML result instantiation exception
 */
static SQLException resultInstantiation(Throwable cause) {

    SQLException ex = Util.sqlException(ErrorCode.GENERAL_ERROR,
        "SQLXML Result instantiation failed: " + cause);

    ex.initCause(cause);

    return ex;
}