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

The following examples show how to use java.sql.SQLException#initCause() . 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
Source Project: lams   File: ResultSetImpl.java    License: 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 2
@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 3
@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 4
Source Project: FoxTelem   File: Blob.java    License: 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 5
/**
 * 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 6
@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 7
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 8
@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 9
Source Project: r-course   File: ServerPreparedStatement.java    License: 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 10
@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 11
@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 12
Source Project: r-course   File: ResultSetScannerInterceptor.java    License: 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 13
@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 14
@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 15
Source Project: r-course   File: Util.java    License: 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 16
@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 17
Source Project: tomcatsrc   File: TrapException.java    License: 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 18
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
/**
 * {@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 20
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;
        }
    }
}