Search in sources :

Example 1 with DelegatingConnection

use of org.apache.commons.dbcp2.DelegatingConnection in project tomcat by apache.

the class PooledConnectionImpl method makeObject.

/**
 * My {@link KeyedPooledObjectFactory} method for creating {@link PreparedStatement}s.
 *
 * @param key
 *            The key for the {@link PreparedStatement} to be created.
 */
@Override
public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws Exception {
    if (null == key) {
        throw new IllegalArgumentException("Prepared statement key is null or invalid.");
    }
    if (key.getStmtType() == StatementType.PREPARED_STATEMENT) {
        final PreparedStatement statement = (PreparedStatement) key.createStatement(connection);
        // Unable to find way to avoid this
        @SuppressWarnings({ "rawtypes", "unchecked" }) final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pStmtPool, delegatingConnection);
        return new DefaultPooledObject<>(pps);
    }
    final CallableStatement statement = (CallableStatement) key.createStatement(connection);
    @SuppressWarnings("unchecked") final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pStmtPool, (DelegatingConnection<Connection>) delegatingConnection);
    return new DefaultPooledObject<>(pcs);
}
Also used : DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) PoolableCallableStatement(org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) DelegatingConnection(org.apache.tomcat.dbcp.dbcp2.DelegatingConnection) DelegatingPreparedStatement(org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement) PreparedStatement(java.sql.PreparedStatement) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement) PoolableCallableStatement(org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement) PoolablePreparedStatement(org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)

Example 2 with DelegatingConnection

use of org.apache.commons.dbcp2.DelegatingConnection in project ANNIS by korpling.

the class AdministrationDao method dumpTableToResource.

@Transactional(readOnly = true)
public void dumpTableToResource(String table, WritableResource resource) {
    log.debug("dumping data to '" + resource.getFilename() + "' from table '" + table + "'");
    String sql = "COPY \"" + table + "\" TO STDOUT WITH DELIMITER E'\t' NULL AS 'NULL'";
    try {
        // 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().copyOut(sql, resource.getOutputStream());
        DataSourceUtils.releaseConnection(originalCon, getDataSource());
    } catch (SQLException e) {
        throw new DatabaseAccessException(e);
    } catch (IOException e) {
        throw new FileAccessException(e);
    }
}
Also used : PGConnection(org.postgresql.PGConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) PGConnection(org.postgresql.PGConnection) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with DelegatingConnection

use of org.apache.commons.dbcp2.DelegatingConnection in project ANNIS by korpling.

the class AdministrationDao method bulkloadTableFromResource.

// bulk-loads a table from a resource
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 {
        // 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);
    }
}
Also used : PGConnection(org.postgresql.PGConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) PGConnection(org.postgresql.PGConnection) IOException(java.io.IOException)

Aggregations

Connection (java.sql.Connection)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 DelegatingConnection (org.apache.commons.dbcp2.DelegatingConnection)2 PGConnection (org.postgresql.PGConnection)2 CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 PooledConnection (javax.sql.PooledConnection)1 DelegatingConnection (org.apache.tomcat.dbcp.dbcp2.DelegatingConnection)1 DelegatingPreparedStatement (org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement)1 PoolableCallableStatement (org.apache.tomcat.dbcp.dbcp2.PoolableCallableStatement)1 PoolablePreparedStatement (org.apache.tomcat.dbcp.dbcp2.PoolablePreparedStatement)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1 Transactional (org.springframework.transaction.annotation.Transactional)1