Example usage for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler

List of usage examples for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler

Introduction

In this page you can find the example usage for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler.

Prototype

RowCallbackHandler

Source Link

Usage

From source file:uk.ac.ebi.atlas.search.diffanalytics.DiffAnalyticsDao.java

public void visitEachExpression(Optional<? extends Collection<IndexedAssayGroup>> indexedContrasts,
        Optional<? extends Collection<String>> geneIds, final Visitor<DiffAnalytics> visitor, String specie) {
    Optional<ImmutableSet<IndexedAssayGroup>> uniqueIndexedContrasts = uniqueIndexedContrasts(indexedContrasts);

    log("visitEachExpression", uniqueIndexedContrasts, geneIds);

    Stopwatch stopwatch = Stopwatch.createStarted();

    String species = "";
    if (StringUtils.isNotBlank(specie)) {
        species = specie;//w w  w.ja  v a  2 s  . co m
    }

    DatabaseQuery<Object> indexedContrastQuery = buildSelect(uniqueIndexedContrasts, geneIds, species);

    final MutableInt count = new MutableInt(0);

    JdbcTemplate template = new JdbcTemplate(dataSource);

    template.setFetchSize(5000);

    try {
        template.query(indexedContrastQuery.getQuery(), new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                count.increment();

                DiffAnalytics dbe = dbeRowMapper.mapRow(resultSet, count.intValue());

                try {
                    visitor.visit(dbe);
                } catch (VisitorException e) {
                    // throw SQLException so result set is closed
                    throw new SQLException(e);
                }
            }

        }, indexedContrastQuery.getParameters().toArray());
    } catch (DataAccessException e) {

        if (e.getCause() != null && e.getCause().getCause() != null
                && e.getCause().getCause() instanceof VisitorException) {
            throw (VisitorException) (e.getCause().getCause());
        } else {
            throw e;
        }
    }

    stopwatch.stop();
    LOGGER.debug(String.format("visitEachExpression processed %s expressions in %s seconds", count.intValue(),
            stopwatch.elapsed(TimeUnit.SECONDS)));
}