Search in sources :

Example 1 with ResultSetIterator

use of org.apache.camel.component.sql.ResultSetIterator in project camel by apache.

the class ElsqlProducer method processStreamList.

protected void processStreamList(final Exchange exchange, final PreparedStatementCreator statementCreator, final String preparedQuery) throws Exception {
    log.trace("processStreamList: {}", preparedQuery);
    // do not use the jdbcTemplate as it will auto-close connection/ps/rs when exiting the execute method
    // and we need to keep the connection alive while routing and close it when the Exchange is done being routed
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        con = dataSource.getConnection();
        ps = statementCreator.createPreparedStatement(con);
        final boolean isResultSet = ps.execute();
        if (isResultSet) {
            rs = ps.getResultSet();
            final ResultSetIterator iterator = getEndpoint().queryForStreamList(con, ps, rs);
            if (getEndpoint().isNoop()) {
                exchange.getOut().setBody(exchange.getIn().getBody());
            } else if (getEndpoint().getOutputHeader() != null) {
                exchange.getOut().setBody(exchange.getIn().getBody());
                exchange.getOut().setHeader(getEndpoint().getOutputHeader(), iterator);
            } else {
                exchange.getOut().setBody(iterator);
            }
            // we do not know the row count so we cannot set a ROW_COUNT header
            // defer closing the iterator when the exchange is complete
            exchange.addOnCompletion(new ResultSetIteratorCompletion(iterator));
        }
    } catch (final Exception e) {
        // in case of exception then close all this before rethrow
        closeConnection(con);
        closeStatement(ps);
        closeResultSet(rs);
        throw e;
    }
}
Also used : ResultSetIterator(org.apache.camel.component.sql.ResultSetIterator) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.springframework.jdbc.support.JdbcUtils.closeConnection) ResultSet(java.sql.ResultSet) JdbcUtils.closeResultSet(org.springframework.jdbc.support.JdbcUtils.closeResultSet) PreparedStatement(java.sql.PreparedStatement) ResultSetIteratorCompletion(org.apache.camel.component.sql.ResultSetIteratorCompletion) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ResultSetIterator (org.apache.camel.component.sql.ResultSetIterator)1 ResultSetIteratorCompletion (org.apache.camel.component.sql.ResultSetIteratorCompletion)1 DataAccessException (org.springframework.dao.DataAccessException)1 JdbcUtils.closeConnection (org.springframework.jdbc.support.JdbcUtils.closeConnection)1 JdbcUtils.closeResultSet (org.springframework.jdbc.support.JdbcUtils.closeResultSet)1