use of org.apache.camel.component.sql.ResultSetIteratorCompletion 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;
}
}
Aggregations