Example usage for org.apache.commons.dbcp2 DelegatingConnection getInnermostDelegate

List of usage examples for org.apache.commons.dbcp2 DelegatingConnection getInnermostDelegate

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 DelegatingConnection getInnermostDelegate.

Prototype

public Connection getInnermostDelegate() 

Source Link

Document

If my underlying Connection is not a DelegatingConnection , returns it, otherwise recursively invokes this method on my delegate.

Usage

From source file:annis.administration.AdministrationDao.java

private void bulkloadTableFromResource(String table, Resource resource) {
    log.debug("bulk-loading data from '" + resource.getFilename() + "' into table '" + table + "'");
    String sql = "COPY " + table + " FROM STDIN WITH DELIMITER E'\t' NULL AS 'NULL'";

    try {// w  w  w  . j ava2  s  . c  o  m
        // retrieve the currently open connection if running inside a transaction
        Connection originalCon = DataSourceUtils.getConnection(getDataSource());
        Connection con = originalCon;
        if (con instanceof DelegatingConnection) {
            DelegatingConnection<?> delCon = (DelegatingConnection<?>) con;
            con = delCon.getInnermostDelegate();
        }

        Preconditions.checkState(con instanceof PGConnection,
                "bulk-loading only works with a PostgreSQL JDBC connection");

        // Postgres JDBC4 8.4 driver now supports the copy API
        PGConnection pgCon = (PGConnection) con;
        pgCon.getCopyAPI().copyIn(sql, resource.getInputStream());

        DataSourceUtils.releaseConnection(originalCon, getDataSource());

    } catch (SQLException e) {
        throw new DatabaseAccessException(e);
    } catch (IOException e) {
        throw new FileAccessException(e);
    }
}