Search in sources :

Example 21 with SQLNonTransientConnectionException

use of java.sql.SQLNonTransientConnectionException in project derby by apache.

the class TestJDBC40Exception method testConnectionException.

public void testConnectionException() throws SQLException {
    Statement stmt = createStatement();
    getConnection().close();
    try {
        stmt.execute("select * from exception1");
        fail("Statement didn't fail.");
    } catch (SQLNonTransientConnectionException cone) {
        assertTrue("Unexpected SQL State: " + cone.getSQLState(), cone.getSQLState().startsWith("08"));
    }
    if (usingEmbedded()) {
        // test exception after database shutdown
        // DERBY-3074
        stmt = createStatement();
        TestConfiguration.getCurrent().shutdownDatabase();
        try {
            stmt.execute("select * from exception1");
            fail("Statement didn't fail.");
        } catch (SQLNonTransientConnectionException cone) {
            assertTrue("Unexpected SQL State: " + cone.getSQLState(), cone.getSQLState().startsWith("08"));
        }
    }
    // DERBY-3075
    if (usingDerbyNetClient()) {
        DataSource ds = JDBCDataSource.getDataSource();
        JDBCDataSource.setBeanProperty(ds, "portNumber", 0);
        try {
            ds.getConnection();
        } catch (SQLNonTransientConnectionException cone) {
            assertTrue("Unexpected SQL State: " + cone.getSQLState(), cone.getSQLState().startsWith("08"));
        }
    }
}
Also used : SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) Statement(java.sql.Statement) J2EEDataSource(org.apache.derbyTesting.junit.J2EEDataSource) DataSource(javax.sql.DataSource) JDBCDataSource(org.apache.derbyTesting.junit.JDBCDataSource)

Example 22 with SQLNonTransientConnectionException

use of java.sql.SQLNonTransientConnectionException in project derby by apache.

the class SQLExceptionFactory method getSQLException.

/**
 * <p>
 * method to construct SQLException
 * version specific drivers can overload this method to create
 * version specific exceptions
 * </p>
 *
 * <p>
 * This implementation creates JDBC 4 exceptions.
 * </p>
 *
 * <pre>
 * SQLSTATE CLASS (prefix)     Exception
 * 0A                          java.sql.SQLFeatureNotSupportedException
 * 08                          java.sql.SQLNonTransientConnectionException
 * 22                          java.sql.SQLDataException
 * 28                          java.sql.SQLInvalidAuthorizationSpecException
 * 40                          java.sql.SQLTransactionRollbackException
 * 42                          java.sql.SQLSyntaxErrorException
 * </pre>
 */
@Override
public SQLException getSQLException(String message, String messageId, SQLException next, int severity, Throwable t, Object... args) {
    String sqlState = StandardException.getSQLStateFromIdentifier(messageId);
    // 
    // Create dummy exception which ferries arguments needed to serialize
    // SQLExceptions across the DRDA network layer.
    // 
    StandardException ferry = wrapArgsForTransportAcrossDRDA(messageId, t, args);
    final SQLException ex;
    if (sqlState.startsWith(SQLState.CONNECTIVITY_PREFIX)) {
        // no derby sqlstate belongs to
        // TransientConnectionException DERBY-3074
        ex = new SQLNonTransientConnectionException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {
        ex = new SQLDataException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {
        if (sqlState.equals(SQLState.LANG_NULL_INTO_NON_NULL))
            ex = new SQLIntegrityConstraintViolationException(message, sqlState, severity, ferry);
        else if (sqlState.equals(SQLState.LANG_CHECK_CONSTRAINT_VIOLATED))
            ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState, severity, ferry, args[1], args[0]);
        else
            ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState, severity, ferry, args[0], args[1]);
    } else if (sqlState.startsWith(SQLState.AUTHORIZATION_SPEC_PREFIX)) {
        ex = new SQLInvalidAuthorizationSpecException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.TRANSACTION_PREFIX)) {
        ex = new SQLTransactionRollbackException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.LSE_COMPILATION_PREFIX)) {
        ex = new SQLSyntaxErrorException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.UNSUPPORTED_PREFIX)) {
        ex = new SQLFeatureNotSupportedException(message, sqlState, severity, ferry);
    } else if (sqlState.equals(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT.substring(0, 5)) || sqlState.equals(SQLState.LOGIN_TIMEOUT.substring(0, 5))) {
        ex = new SQLTimeoutException(message, sqlState, severity, ferry);
    } else {
        ex = new SQLException(message, sqlState, severity, ferry);
    }
    // If the argument ferry has recorded any extra next exceptions,
    // graft them into the parent exception.
    SQLException ferriedExceptions = ferry.getNextException();
    if (ferriedExceptions != null) {
        ex.setNextException(ferriedExceptions);
    }
    if (next != null) {
        ex.setNextException(next);
    }
    return ex;
}
Also used : SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) StandardException(org.apache.derby.shared.common.error.StandardException) SQLTransactionRollbackException(java.sql.SQLTransactionRollbackException) SQLDataException(java.sql.SQLDataException) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DerbySQLIntegrityConstraintViolationException(org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLInvalidAuthorizationSpecException(java.sql.SQLInvalidAuthorizationSpecException) DerbySQLIntegrityConstraintViolationException(org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException)

Aggregations

SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)22 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 SQLException (java.sql.SQLException)6 PreparedStatement (java.sql.PreparedStatement)4 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 StatementEvent (javax.sql.StatementEvent)2 StatementEventListener (javax.sql.StatementEventListener)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLDataException (java.sql.SQLDataException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 SQLInvalidAuthorizationSpecException (java.sql.SQLInvalidAuthorizationSpecException)1 SQLNonTransientException (java.sql.SQLNonTransientException)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 SQLTransactionRollbackException (java.sql.SQLTransactionRollbackException)1 Statement (java.sql.Statement)1