Java Code Examples for java.sql.SQLException#getCause()

The following examples show how to use java.sql.SQLException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyncProviderExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test06() {
    SQLException ex = new SyncProviderException("Exception 1");
    ex.initCause(t1);
    SyncProviderException ex1 = new SyncProviderException("Exception 2");
    SyncProviderException ex2 = new SyncProviderException("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
Source File: SyncProviderExceptionTests.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test06() {
    SQLException ex = new SyncProviderException("Exception 1");
    ex.initCause(t1);
    SyncProviderException ex1 = new SyncProviderException("Exception 2");
    SyncProviderException ex2 = new SyncProviderException("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
Source File: BatchUpdateExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test16() {
    BatchUpdateException ex = new BatchUpdateException("Exception 1", uc,  t1);
    BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc);
    BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    SQLException sqe = ex;
    int num = 0;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 4
Source File: RowSetWarningTests.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test07() {
    RowSetWarning ex = new RowSetWarning("Exception 1");
    ex.initCause(t1);
    RowSetWarning ex1 = new RowSetWarning("Exception 2");
    RowSetWarning ex2 = new RowSetWarning("Exception 3");
    ex2.initCause(t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 5
Source File: DataTruncationTests.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct using
 * traditional while loop
 */
@Test
public void test12() {
    DataTruncation ex = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize, t1);
    DataTruncation ex1 = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize);
    DataTruncation ex2 = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize, t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(dtmsgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(dtmsgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 6
Source File: SerialExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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 7
Source File: CreateHDFSStoreTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void testReplicatedHDFSStore() throws Exception {
  Properties props = new Properties();
  int mcastPort = AvailablePort.getRandomAvailablePort(AvailablePort.JGROUPS);
  props.put("mcast-port", String.valueOf(mcastPort));
  props.put(DistributionConfig.MCAST_TTL_NAME, "0");
  Connection conn = TestUtil.getConnection(props);
  Statement st = conn.createStatement();

  checkDirExistence("./myhdfs");
  st.execute("create hdfsstore myhdfs namenode 'hdfs://127.0.0.1:" + port + "' homedir './myhdfs'");
  try {
    st.execute("create table mytab (col1 int primary key) replicate hdfsstore (myhdfs) enable concurrency checks");
    fail("Expected SQLException");
  } catch (SQLException se) {
    if (se.getCause() instanceof UnsupportedOperationException) {
      assertTrue(se
          .getMessage()
          .contains(
              LocalizedStrings.HDFSSTORE_IS_USED_IN_REPLICATED_TABLE
                  .toLocalizedString()));
    }
    se.printStackTrace();
  }
  st.execute("drop hdfsstore myhdfs");
  delete(new File("./myhdfs"));
}
 
Example 8
Source File: SQLFeatureNotSupportedExceptionTests.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLFeatureNotSupportedException ex =
            new SQLFeatureNotSupportedException("Exception 1", t1);
    SQLFeatureNotSupportedException ex1 =
            new SQLFeatureNotSupportedException("Exception 2");
    SQLFeatureNotSupportedException ex2 =
            new SQLFeatureNotSupportedException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 9
Source File: SQLClientInfoExceptionTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct using
 * traditional while loop
 */
@Test
public void test12() {
    SQLClientInfoException ex = new SQLClientInfoException("Exception 1",
            map, t1);
    SQLClientInfoException ex1 = new SQLClientInfoException("Exception 2",
            map);
    SQLClientInfoException ex2 = new SQLClientInfoException("Exception 3",
            map, t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 10
Source File: SQLIntegrityConstraintViolationExceptionTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLIntegrityConstraintViolationException ex =
            new SQLIntegrityConstraintViolationException("Exception 1", t1);
    SQLIntegrityConstraintViolationException ex1 =
            new SQLIntegrityConstraintViolationException("Exception 2");
    SQLIntegrityConstraintViolationException ex2 =
            new SQLIntegrityConstraintViolationException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 11
Source File: DataTruncationTests.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct using
 * traditional while loop
 */
@Test
public void test12() {
    DataTruncation ex = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize, t1);
    DataTruncation ex1 = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize);
    DataTruncation ex2 = new DataTruncation(index, parameter, onRead,
            dataSize, transferSize, t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(dtmsgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(dtmsgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 12
Source File: SQLFeatureNotSupportedExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLFeatureNotSupportedException ex =
            new SQLFeatureNotSupportedException("Exception 1", t1);
    SQLFeatureNotSupportedException ex1 =
            new SQLFeatureNotSupportedException("Exception 2");
    SQLFeatureNotSupportedException ex2 =
            new SQLFeatureNotSupportedException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 13
Source File: SQLNonTransientExceptionTests.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLNonTransientException ex = new SQLNonTransientException("Exception 1", t1);
    SQLNonTransientException ex1 = new SQLNonTransientException("Exception 2");
    SQLNonTransientException ex2 = new SQLNonTransientException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 14
Source File: BatchUpdateExceptionTests.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test16() {
    BatchUpdateException ex = new BatchUpdateException("Exception 1", uc,  t1);
    BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc);
    BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    SQLException sqe = ex;
    int num = 0;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 15
Source File: BaseJDBCTestCase.java    From kareldb with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that the two (2) passed-in SQLException's are equals and
 * not just '=='.
 *
 * @param se1 first SQLException to compare
 * @param se2 second SQLException to compare
 */
public static void assertSQLExceptionEquals(SQLException se1,
                                            SQLException se2) {
    // Ensure non-null SQLException's are being passed.
    assertNotNull(
        "Passed-in SQLException se1 cannot be null",
        se1);
    assertNotNull(
        "Passed-in SQLException se2 cannot be null",
        se2);

    // Now verify that the passed-in SQLException's are of the same type
    assertEquals("SQLException class types are different",
        se1.getClass().getName(), se2.getClass().getName());

    // Here we check that the detailed message of both
    // SQLException's is the same
    assertEquals(
        "Detailed messages of the SQLException's are different",
        se1.getMessage(), se2.getMessage());

    // Check that getCause() returns the same value on the two exceptions.
    Throwable se1Cause = se1.getCause();
    Throwable se2Cause = se2.getCause();
    if (se1Cause == null) {
        assertNull(se2Cause);
    } else {
        assertEquals(se1Cause, se2Cause);
    }

    // Check that the two exceptions have the same next exception.
    if (se1.getNextException() == null) {
        assertNull(se2.getNextException());
    } else {
        assertSQLExceptionEquals(se1.getNextException(),
            se2.getNextException());
    }
}
 
Example 16
Source File: SQLError.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public static SQLException createSQLException(String message, String sqlState, Throwable cause, ExceptionInterceptor interceptor) {
    SQLException sqlEx = createSQLException(message, sqlState, null);

    if (sqlEx.getCause() == null) {
        if (cause != null) {
            try {
                sqlEx.initCause(cause);
            } catch (Throwable t) {
                // we're not going to muck with that here, since it's an error condition anyway!
            }
        }
    }
    // Run through the exception interceptor after setting the init cause.
    return runThroughExceptionInterceptor(interceptor, sqlEx);
}
 
Example 17
Source File: Validator.java    From presto with Apache License 2.0 5 votes vote down vote up
private static boolean isPrestoQueryInvalid(SQLException e)
{
    for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
        if (t.toString().contains(".SemanticException:")) {
            return true;
        }
        if (t.toString().contains(".ParsingException:")) {
            return true;
        }
        if (nullToEmpty(t.getMessage()).matches("Function .* not registered")) {
            return true;
        }
    }
    return false;
}
 
Example 18
Source File: TestExecutionExceptionsToClient.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private boolean hasType(SQLException e, ErrorType errorType) {
  UserRemoteException userException = ((UserRemoteException) e.getCause());
  return userException.getErrorType() == errorType;
}
 
Example 19
Source File: RuntimeSQLException.java    From vertx-jdbc-client with Apache License 2.0 4 votes vote down vote up
public RuntimeSQLException(SQLException exception) {
  super(exception.getMessage(), exception.getCause());
}
 
Example 20
Source File: DatabaseRecordLookupService.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<Record> lookup(final Map<String, Object> coordinates, Map<String, String> context) throws LookupFailureException {
    if (coordinates == null) {
        return Optional.empty();
    }

    final Object key = coordinates.get(KEY);
    if (StringUtils.isBlank(key.toString())) {
        return Optional.empty();
    }

    final String tableName = getProperty(TABLE_NAME).evaluateAttributeExpressions(context).getValue();
    final String lookupValueColumnsList = getProperty(LOOKUP_VALUE_COLUMNS).evaluateAttributeExpressions(context).getValue();

    Set<String> lookupValueColumnsSet = new LinkedHashSet<>();
    if (lookupValueColumnsList != null) {
        Stream.of(lookupValueColumnsList)
                .flatMap(path -> Arrays.stream(path.split(",")))
                .filter(DatabaseRecordLookupService::isNotBlank)
                .map(String::trim)
                .forEach(lookupValueColumnsSet::add);
    }

    final String lookupValueColumns = lookupValueColumnsSet.isEmpty() ? "*" : String.join(",", lookupValueColumnsSet);

    Tuple<String, Object> cacheLookupKey = new Tuple<>(tableName, key);

    // Not using the function param of cache.get so we can catch and handle the checked exceptions
    Record foundRecord = cache.get(cacheLookupKey, k -> null);

    if (foundRecord == null) {
        final String selectQuery = "SELECT " + lookupValueColumns + " FROM " + tableName + " WHERE " + lookupKeyColumn + " = ?";
        try (final Connection con = dbcpService.getConnection(context);
             final PreparedStatement st = con.prepareStatement(selectQuery)) {

            st.setObject(1, key);
            ResultSet resultSet = st.executeQuery();
            ResultSetRecordSet resultSetRecordSet = new ResultSetRecordSet(resultSet, null);
            foundRecord = resultSetRecordSet.next();

            // Populate the cache if the record is present
            if (foundRecord != null) {
                cache.put(cacheLookupKey, foundRecord);
            }

        } catch (SQLException se) {
            throw new LookupFailureException("Error executing SQL statement: " + selectQuery + "for value " + key.toString()
                    + " : " + (se.getCause() == null ? se.getMessage() : se.getCause().getMessage()), se);
        } catch (IOException ioe) {
            throw new LookupFailureException("Error retrieving result set for SQL statement: " + selectQuery + "for value " + key.toString()
                    + " : " + (ioe.getCause() == null ? ioe.getMessage() : ioe.getCause().getMessage()), ioe);
        }
    }

    return Optional.ofNullable(foundRecord);
}