Example usage for java.sql SQLException setNextException

List of usage examples for java.sql SQLException setNextException

Introduction

In this page you can find the example usage for java.sql SQLException setNextException.

Prototype

public void setNextException(SQLException ex) 

Source Link

Document

Adds an SQLException object to the end of the chain.

Usage

From source file:org.datacleaner.job.runner.ErrorAwareAnalysisListenerTest.java

public void testHandleErrorWithSqlNextException() throws Exception {
    ErrorAwareAnalysisListener listener = new ErrorAwareAnalysisListener();
    SQLException sqlException = new SQLException("foo", new IllegalStateException("bar"));
    sqlException.setNextException(new SQLException("baz"));

    listener.handleError(null, new MetaModelException(sqlException));

    String string = FileHelper.readInputStreamAsString(new ByteArrayInputStream(baos.toByteArray()), "UTF8");
    assertTrue(string,//from ww  w .j a  va2 s.c o  m
            string.indexOf("org.apache.metamodel.MetaModelException: java.sql.SQLException: foo") != -1);

    assertTrue(string, string
            .indexOf("WARN  ErrorAwareAnalysisListener - SQLException.getNextException() stack trace:") != -1);

    assertTrue(string, string.indexOf("java.sql.SQLException: baz") != -1);

}

From source file:org.enhydra.jdbc.pool.StandardPoolDataSource.java

/**
 * getConnection allows to get an object from the pool and returns it
 * to the user. In this case, we return an PooledConnection
 *///from   www  . j  a  v  a 2 s .c om
public Connection getConnection(String _user, String _password) throws SQLException {
    log.debug("StandardPoolDataSource:getConnection");
    Connection ret = null;
    PooledConnection con = null;

    synchronized (this) {
        if (!onOff) {
            log.debug("StandardPoolDataSource:getConnection must configure the pool...");
            pool.start(); // the pool starts now
            onOff = true; // and is initialized
            log.debug("StandardPoolDataSource:getConnection pool config : \n" + pool.toString());
        }
    }

    try {
        try {
            log.debug("StandardPoolDataSource:getConnection Try to give a " + "connection (checkOut)");
            con = (PooledConnection) pool.checkOut(_user, _password);
            // get a connection from the pool
            log.debug("StandardPoolDataSource:getConnection checkOut return" + "a new connection");
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(
                    "StandardPoolDataSource:getConnection SQLException in StandardPoolDataSource:getConnection"
                            + e);
            throw new SQLException(
                    "SQLException in StandardPoolDataSource:getConnection no connection available " + e);
        }

        ret = con.getConnection();
    } catch (Exception e) {
        log.debug("StandardPoolDataSource:getConnection exception" + e);
        e.printStackTrace();
        SQLException sqle = new SQLException(
                "SQLException in StandardPoolDataSource:getConnection exception: " + e);
        if (e instanceof SQLException)
            sqle.setNextException((SQLException) e);
        if (con != null) {
            pool.checkIn(con);
        }
        throw sqle;
    }
    log.debug("StandardPoolDataSource:getConnection return a connection");
    return ret;
}

From source file:org.eobjects.analyzer.job.runner.ErrorAwareAnalysisListenerTest.java

public void testHandleErrorWithSqlNextException() throws Exception {
    ErrorAwareAnalysisListener listener = new ErrorAwareAnalysisListener();
    SQLException sqlException = new SQLException("foo", new IllegalStateException("bar"));
    sqlException.setNextException(new SQLException("baz"));

    listener.handleError(null, new MetaModelException(sqlException));

    String string = FileHelper.readInputStreamAsString(new ByteArrayInputStream(baos.toByteArray()), "UTF8");
    assertTrue(string,//from  w  ww .j  ava2  s . c  o m
            string.indexOf("org.eobjects.metamodel.MetaModelException: java.sql.SQLException: foo") != -1);

    assertTrue(string, string
            .indexOf("WARN  ErrorAwareAnalysisListener - SQLException.getNextException() stack trace:") != -1);

    assertTrue(string, string.indexOf("java.sql.SQLException: baz") != -1);

}

From source file:xbird.util.jdbc.QueryRunner.java

/**
 * Throws a new exception with a more informative error message.
 * //from w w  w  . j av  a2  s .c  o  m
 * @param cause The original exception that will be chained to the new 
 * exception when it's rethrown. 
 * @param sql The query that was executing when the exception happened.     
 * @param params The query replacement paramaters; <code>null</code> is a 
 * valid value to pass in.
 */
protected void rethrow(SQLException cause, String sql, Object... params) throws SQLException {
    StringBuffer msg = new StringBuffer(cause.getMessage());
    msg.append(" Query: ");
    msg.append(sql);
    msg.append(" Parameters: ");
    if (params == null) {
        msg.append("[]");
    } else {
        msg.append(Arrays.asList(params));
    }
    SQLException e = new SQLException(msg.toString());
    e.setNextException(cause);
    throw e;
}

From source file:xbird.util.jdbc.QueryRunner.java

protected void rethrow(SQLException cause, String... sqls) throws SQLException {
    StringBuffer msg = new StringBuffer(cause.getMessage());
    msg.append(" Query: ");
    if (sqls == null) {
        msg.append("[]");
    } else {//from w  w w .  j  av  a 2  s  . co  m
        msg.append(Arrays.asList(sqls));
    }
    SQLException e = new SQLException(msg.toString());
    e.setNextException(cause);
    throw e;
}