Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Apache Commons DBCP 2 (and Commons Pool 2) #1022

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Occam Labs UG (haftungsbeschränkt)

/**
* {@link ConnectionProvider} based on <code>javax.sql.DataSource</code>.
*
*
* @author <a href="mailto:schneider@occamlabs.de">Markus Schneider</a>
*
*
* @since 3.4
*/
class DataSourceConnectionProvider implements ConnectionProvider {
Expand All @@ -76,7 +76,7 @@ class DataSourceConnectionProvider implements ConnectionProvider {

/**
* Creates a new {@link DataSourceConnectionProvider} instance.
*
*
* @param resourceMetadata
* metadata, must not be <code>null</code>
* @param ds
Expand Down Expand Up @@ -118,12 +118,15 @@ public Connection getConnection() {
@Override
public void destroy() {
if ( destroyMethod != null ) {
LOG.info("Closing connection pool {}", resourceMetadata.getIdentifier());
try {
destroyMethod.invoke( ds );
} catch ( Exception e ) {
String msg = "Error destroying DataSource instance: " + e.getLocalizedMessage();
LOG.error( msg );
LOG.error( "Error destroying DataSource instance: {}", e.getLocalizedMessage() );
}
} else {
LOG.warn( "Unable to close connection pool {}. Check the DataSource configuration if the attribute "
+ "'destroyMethod' is configured.", resourceMetadata.getIdentifier() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://www.deegree.org/connectionprovider/datasource http://schemas.deegree.org/jdbc/datasource/3.4.0/datasource.xsd">

<!-- Creation / lookup of javax.sql.DataSource instance -->
<DataSource javaClass="org.apache.commons.dbcp.BasicDataSource" />
<DataSource javaClass="org.apache.commons.dbcp2.BasicDataSource" />

<!-- Configuration of DataSource properties -->
<Property name="driverClassName" value="com.mysql.jdbc.Driver" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://www.deegree.org/connectionprovider/datasource http://schemas.deegree.org/jdbc/datasource/3.4.0/datasource.xsd">

<!-- Creation / lookup of javax.sql.DataSource instance -->
<DataSource javaClass="org.apache.commons.dbcp.BasicDataSource" />
<DataSource javaClass="org.apache.commons.dbcp2.BasicDataSource" />

<!-- Configuration of DataSource properties -->
<Property name="driverClassName" value="oracle.jdbc.OracleDriver" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://www.deegree.org/connectionprovider/datasource http://schemas.deegree.org/jdbc/datasource/3.4.0/datasource.xsd">

<!-- Creation / lookup of javax.sql.DataSource instance -->
<DataSource javaClass="org.apache.commons.dbcp.BasicDataSource" />
<DataSource javaClass="org.apache.commons.dbcp2.BasicDataSource" />

<!-- Configuration of DataSource properties -->
<Property name="driverClassName" value="org.postgresql.Driver" />
Expand Down
8 changes: 4 additions & 4 deletions deegree-core/deegree-core-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@
<artifactId>Saxon-HE</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>jogl</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DelegatingConnection;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.deegree.commons.annotations.LoggingNotes;
import org.slf4j.Logger;

/**
* Simple implementation of a JDBC connection pool based on the Apache Commons Pool and DBCP projects.
*
*
* @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
* @author last edited by: $Author: schneider $
*
* @version $Revision: $, $Date: $
*/
@LoggingNotes(debug = "logs information about pool usage")
Expand All @@ -67,11 +66,11 @@ public class ConnectionPool {

private final PoolingDataSource ds;

private final GenericObjectPool<Connection> pool;
private final GenericObjectPool<PoolableConnection> pool;

/**
* Creates a new {@link ConnectionPool} instance.
*
*
* @param id
* @param connectURI
* @param user
Expand All @@ -84,14 +83,16 @@ public ConnectionPool( String id, String connectURI, String user, String passwor
int maxActive ) {

this.id = id;
pool = new GenericObjectPool<Connection>( null );
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( connectURI, user, password );
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, null );
pool = new GenericObjectPool<>( poolableConnectionFactory );
pool.setMinIdle( minIdle );
pool.setMaxActive( maxActive );
pool.setMaxTotal( maxActive );
pool.setTestOnBorrow( true );

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( connectURI, user, password );
// TODO make this configurable
new PoolableConnectionFactory( connectionFactory, pool, null, null, readOnly, true );
poolableConnectionFactory.setPool( pool );
ds = new PoolingDataSource( pool );

// needed, so users can retrieve the underlying connection from pooled
// connections, e.g. to access the
// LargeObjectManager from a PGConnection
Expand All @@ -100,7 +101,7 @@ public ConnectionPool( String id, String connectURI, String user, String passwor

/**
* Returns a {@link Connection} from the pool.
*
*
* @return a connection from the pool
* @throws SQLException
*/
Expand All @@ -117,11 +118,13 @@ public Connection getConnection()
public void destroy()
throws Exception {
pool.close();
ds.close();
}

public void invalidate( DelegatingConnection conn )
public void invalidate( PoolableConnection conn )
throws Exception {
conn.getDelegate().close();
conn.reallyClose();
pool.invalidateObject( conn );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Occam Labs UG (haftungsbeschränkt)
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.DelegatingConnection;
import org.apache.commons.dbcp2.DelegatingConnection;
import org.apache.commons.dbcp2.PoolableConnection;
import org.deegree.commons.jdbc.ConnectionPool;
import org.deegree.db.ConnectionProvider;
import org.deegree.sqldialect.SQLDialect;
Expand Down Expand Up @@ -128,7 +129,7 @@ public SQLDialect getDialect() {
@Override
public void invalidate( Connection conn ) {
try {
pool.invalidate( (DelegatingConnection) conn );
pool.invalidate( (PoolableConnection) conn );
} catch ( Exception e ) {
throw new RuntimeException( e );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Occam Labs UG (haftungsbeschränkt)
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.deegree.feature.FeatureCollection;
import org.deegree.geometry.Envelope;
import org.deegree.tile.Tile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Occam Labs UG (haftungsbeschränkt)
import java.io.File;
import java.util.List;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.deegree.geometry.Envelope;
import org.deegree.geometry.GeometryFactory;
import org.deegree.tile.TileDataLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,52 @@ Occam Labs UG (haftungsbeschränkt)
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
* <code>ImageReaderFactory</code>: an object factory for commons-pool. It should really be replaced with a better
* solution, not using generics here (and throwing Exception) is not the way to go...
* <code>ImageReaderFactory</code>: an object factory for Apache commons-pool providing
* file-based image reader.
*
* @author <a href="mailto:schmitz@occamlabs.de">Andreas Schmitz</a>
* @author last edited by: $Author: mschneider $
*
* @version $Revision: 31882 $, $Date: 2011-09-15 02:05:04 +0200 (Thu, 15 Sep 2011) $
* @author <a href="mailto:friebe@lat-lon.de">Torsten Friebe</a>
*
*/
public class ImageReaderFactory implements PooledObjectFactory<ImageReader> {

public class ImageReaderFactory implements PoolableObjectFactory {

private File file;
private final File file;

public ImageReaderFactory( File file ) {
this.file = file;
}

@Override
public void activateObject( Object o )
throws Exception {
public void destroyObject(PooledObject pooledObject) throws Exception {
ImageReader reader = (ImageReader) pooledObject;
reader.dispose();
}

@Override
public boolean validateObject(PooledObject pooledObject) {
// ImageReader reader = (ImageReader) o;
// ImageInputStream iis = (ImageInputStream) reader.getInput();
// unknown if we need something here, so far no readers have become invalid
return true;
}

@Override
public void activateObject(PooledObject pooledObject) throws Exception {
// nothing to do
}

@Override
public void destroyObject( Object o )
throws Exception {
ImageReader reader = (ImageReader) o;
reader.dispose();
public void passivateObject(PooledObject pooledObject) throws Exception {
// nothing to do
}

@Override
public Object makeObject()
throws Exception {
public PooledObject<ImageReader> makeObject() throws Exception {
ImageInputStream iis = null;
ImageReader reader = null;
Iterator<ImageReader> readers = getImageReadersBySuffix( "tiff" );
Expand All @@ -95,21 +105,6 @@ public Object makeObject()
iis = createImageInputStream( file );
// already checked in provider
reader.setInput( iis );
return reader;
return new DefaultPooledObject(reader);
}

@Override
public void passivateObject( Object o )
throws Exception {
// nothing to do
}

@Override
public boolean validateObject( Object o ) {
// ImageReader reader = (ImageReader) o;
// ImageInputStream iis = (ImageInputStream) reader.getInput();
// unknown if we need something here, so far no readers have become invalid
return true;
}

}
Loading