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

The following examples show how to use java.sql.SQLException#initCause() . 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: ServerPreparedStatement.java    From r-course with MIT License 6 votes vote down vote up
@Override
protected PreparedStatement prepareBatchedInsertSQL(MySQLConnection localConn, int numBatches) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        try {
            PreparedStatement pstmt = ((Wrapper) localConn.prepareStatement(this.parseInfo.getSqlForBatch(numBatches), this.resultSetType,
                    this.resultSetConcurrency)).unwrap(PreparedStatement.class);
            pstmt.setRetrieveGeneratedKeys(this.retrieveGeneratedKeys);

            return pstmt;
        } catch (UnsupportedEncodingException e) {
            SQLException sqlEx = SQLError.createSQLException("Unable to prepare batch statement", SQLError.SQL_STATE_GENERAL_ERROR,
                    getExceptionInterceptor());
            sqlEx.initCause(e);

            throw sqlEx;
        }
    }
}
 
Example 2
Source File: SyncProviderExceptionTests.java    From jdk8u_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: Util.java    From r-course with MIT License 6 votes vote down vote up
/**
 * Returns initialized instances of classes listed in extensionClassNames.
 * There is no need to call Extension.init() method after that if you don't change connection or properties.
 * 
 * @param conn
 * @param props
 * @param extensionClassNames
 * @param errorMessageKey
 * @param exceptionInterceptor
 * @throws SQLException
 */
public static List<Extension> loadExtensions(Connection conn, Properties props, String extensionClassNames, String errorMessageKey,
        ExceptionInterceptor exceptionInterceptor) throws SQLException {
    List<Extension> extensionList = new LinkedList<Extension>();

    List<String> interceptorsToCreate = StringUtils.split(extensionClassNames, ",", true);

    String className = null;

    try {
        for (int i = 0, s = interceptorsToCreate.size(); i < s; i++) {
            className = interceptorsToCreate.get(i);
            Extension extensionInstance = (Extension) Class.forName(className).newInstance();
            extensionInstance.init(conn, props);

            extensionList.add(extensionInstance);
        }
    } catch (Throwable t) {
        SQLException sqlEx = SQLError.createSQLException(Messages.getString(errorMessageKey, new Object[] { className }), exceptionInterceptor);
        sqlEx.initCause(t);

        throw sqlEx;
    }

    return extensionList;
}
 
Example 4
Source File: SerialExceptionTests.java    From jdk8u-jdk 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 5
Source File: SerialExceptionTests.java    From openjdk-jdk8u 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 6
Source File: ResultSetScannerInterceptor.java    From r-course with MIT License 6 votes vote down vote up
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 7
Source File: SyncProviderExceptionTests.java    From hottub 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 8
Source File: SyncProviderExceptionTests.java    From jdk8u60 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 9
Source File: ResultSetImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public java.io.Reader getCharacterStream(int columnIndex) throws SQLException {
    checkRowPos();
    checkColumnBounds(columnIndex);
    InputStream stream = getBinaryStream(columnIndex);

    if (stream == null) {
        return null;
    }

    Field f = this.columnDefinition.getFields()[columnIndex - 1];
    try {
        return new InputStreamReader(stream, f.getEncoding());
    } catch (UnsupportedEncodingException e) {
        SQLException sqlEx = SQLError.createSQLException("Cannot read value with encoding: " + f.getEncoding(), this.exceptionInterceptor);
        sqlEx.initCause(e);
        throw sqlEx;
    }
}
 
Example 10
Source File: SerialExceptionTests.java    From TencentKona-8 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 11
Source File: JdbcPoolDataSource.java    From XPagesExtensionLibrary with Apache License 2.0 6 votes vote down vote up
public Connection getConnection() throws SQLException {
    try {
        JdbcPoolConnection c = (JdbcPoolConnection)pool.obtainConnection();
        // If the connection is not valid, this maybe because there is a change on a remote server
        // In this case, then clear-up the pool and retry... 
        if(!isValidConnection(c)) {
            pool.reset(pool.getGenerationId());
            c = (JdbcPoolConnection)pool.obtainConnection();
        }
        return c;
    } catch(PoolException ex) {
        SQLException sqx = new SQLException("Error while creating connection"); // $NLX-JdbcPoolDataSource.Errorwhilecreatingconnection-1$
        sqx.initCause(ex);
        throw sqx;
    }
}
 
Example 12
Source File: ByteArrayRow.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Reader getReader(int columnIndex) throws SQLException {
    InputStream stream = getBinaryInputStream(columnIndex);

    if (stream == null) {
        return null;
    }

    try {
        return new InputStreamReader(stream, this.metadata[columnIndex].getEncoding());
    } catch (UnsupportedEncodingException e) {
        SQLException sqlEx = SQLError.createSQLException("", this.exceptionInterceptor);

        sqlEx.initCause(e);

        throw sqlEx;
    }
}
 
Example 13
Source File: SingleByteCharsetConverter.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initialize the shared instance of a converter for the given character
 * encoding.
 * 
 * @param javaEncodingName
 *            the Java name for the character set to initialize
 * @return a converter for the given character set
 * @throws UnsupportedEncodingException
 *             if the character encoding is not supported
 */
public static SingleByteCharsetConverter initCharset(String javaEncodingName) throws UnsupportedEncodingException, SQLException {
    try {
        if (CharsetMapping.isMultibyteCharset(javaEncodingName)) {
            return null;
        }
    } catch (RuntimeException ex) {
        SQLException sqlEx = SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, null);
        sqlEx.initCause(ex);
        throw sqlEx;
    }

    SingleByteCharsetConverter converter = new SingleByteCharsetConverter(javaEncodingName);

    CONVERTER_MAP.put(javaEncodingName, converter);

    return converter;
}
 
Example 14
Source File: Blob.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Override
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"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);
        sqlEx.initCause(ioEx);

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

    return length;
}
 
Example 15
Source File: ServerPreparedStatement.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected ClientPreparedStatement prepareBatchedInsertSQL(JdbcConnection localConn, int numBatches) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        try {
            ClientPreparedStatement pstmt = ((Wrapper) localConn.prepareStatement(((PreparedQuery<?>) this.query).getParseInfo().getSqlForBatch(numBatches),
                    this.resultSetConcurrency, this.query.getResultType().getIntValue())).unwrap(ClientPreparedStatement.class);
            pstmt.setRetrieveGeneratedKeys(this.retrieveGeneratedKeys);

            return pstmt;
        } catch (UnsupportedEncodingException e) {
            SQLException sqlEx = SQLError.createSQLException(Messages.getString("ServerPreparedStatement.27"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
            sqlEx.initCause(e);

            throw sqlEx;
        }
    }
}
 
Example 16
Source File: SyncFactoryExceptionTests.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 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 17
Source File: DefaultExceptionFactory30.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public SQLException getSQLException(String message, String sqlState,
    int errCode, SQLException next, Throwable t) {
  SQLException sqle = new SQLException(
      getMessage(message, sqlState, errCode), sqlState, errCode);
  if (next != null) {
    sqle.setNextException(next);
  }
  if (t != null) {
    sqle.initCause(t);
  }
  return sqle;
}
 
Example 18
Source File: ConnectionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void throwConnectionClosedException() throws SQLException {
    SQLException ex = SQLError.createSQLException(Messages.getString("Connection.2"), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN,
            getExceptionInterceptor());

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

    throw ex;
}
 
Example 19
Source File: TrapException.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        return super.invoke(proxy, method, args);
    }catch (Exception t) {
        Throwable exception = t;
        if (t instanceof InvocationTargetException && t.getCause() != null) {
            exception = t.getCause();
            if (exception instanceof Error) {
                throw exception;
            }
        }
        Class<?> exceptionClass = exception.getClass();
        if (!isDeclaredException(method, exceptionClass)) {
            if (isDeclaredException(method,SQLException.class)) {
                SQLException sqlx = new SQLException("Uncaught underlying exception.");
                sqlx.initCause(exception);
                exception = sqlx;
            } else {
                RuntimeException rx = new RuntimeException("Uncaught underlying exception.");
                rx.initCause(exception);
                exception = rx;
            }
        }
        throw exception;
    }

}
 
Example 20
Source File: XAConnectionPoolingDataSource.java    From reladomo with Apache License 2.0 4 votes vote down vote up
private XAConnectionWrapper getFreshConnectionFromPool()
        throws SQLException
{
    XAConnectionWrapper result;
    Connection freshConnectionFromPool = null;
    while(true)
    {
        DatabaseType databaseType = getDatabaseType();
        try
        {
            freshConnectionFromPool = XAConnectionPoolingDataSource.this.getConnectionFromPool();
            result = new XAConnectionWrapper(freshConnectionFromPool);
            result.setAutoCommit(false);
            result.setTransactionIsolation(databaseType.zGetTxLevel());
            result.setResource(this);
            return result;
        }
        catch (SQLException e)
        {
            if (freshConnectionFromPool != null)
            {
                boolean isDead = false;
                if (databaseType != null && databaseType.isConnectionDead(e))
                {
                    logger.warn("detected dead connection, closing and retrying");
                    try
                    {
                        getPool().invalidateObject(freshConnectionFromPool);
                        isDead = true;
                    }
                    catch (Exception e1)
                    {
                        SQLException sqlException = new SQLException("could not invalidate bad connection ");
                        sqlException.initCause(e1);
                        throw sqlException;
                    }
                }
                else
                {
                    freshConnectionFromPool.close();
                }
                if (isDead) continue;
            }
            throw e;
        }
    }
}