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

The following examples show how to use java.sql.SQLException#getCause() . These examples are extracted from open source projects. 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
@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
/**
 * 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 3
@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 4
/**
 * 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 5
@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 6
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 7
/**
 * 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 8
/**
 * 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 9
/**
 * 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 10
/**
 * 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 11
/**
 * 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 12
/**
 * 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 13
/**
 * 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 14
@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 15
Source Project: presto   File: Validator.java    License: 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 16
Source Project: kareldb   File: BaseJDBCTestCase.java    License: 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 17
Source Project: FoxTelem   File: SQLError.java    License: 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 18
public RuntimeSQLException(SQLException exception) {
  super(exception.getMessage(), exception.getCause());
}
 
Example 19
private boolean hasType(SQLException e, ErrorType errorType) {
  UserRemoteException userException = ((UserRemoteException) e.getCause());
  return userException.getErrorType() == errorType;
}
 
Example 20
@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);
}