Skip to content

Commit

Permalink
External Datasource implementation support
Browse files Browse the repository at this point in the history
  • Loading branch information
asimarslan committed Dec 14, 2012
1 parent 17f533e commit 8826758
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 16 deletions.
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2012 - Batoo Software ve Consultancy Ltd.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.batoo.jpa.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
* Abstract class for implementing pluggable data source
*
* @author asimarslan
* @since $version
*/
public abstract class AbstractDataSource implements DataSource {

/**
* finalize the underlining implementation
*
* @since $version
*/
public abstract void close();

/**
* {@inheritDoc}
*
*/
@Override
public abstract Connection getConnection() throws SQLException;

/**
* initialize the underlining implementation
*
* @param persistanceUnitName
* @param hintName
* @since $version
*/

public abstract void open(String persistanceUnitName, String hintName);

/**
* release the connection
*
* @param connection
* @since $version
*/
public abstract void releaseConnection(Connection connection);

}
19 changes: 18 additions & 1 deletion batoo-jdbc/src/main/java/org/batoo/jpa/jdbc/ConnectionProxy.java
Expand Up @@ -49,10 +49,21 @@ public class ConnectionProxy implements Connection {

private final Connection connection;

private final AbstractDataSource dataSourcePool;

private final long slowSqlThreshold;
private final SqlLoggingType sqlLogging;
private final int jdbcFetchSize;

public ConnectionProxy(AbstractDataSource dataSourcePool, Connection connection, long slowSqlThreshold, SqlLoggingType sqlLogging, int jdbcFetchSize) {
super();
this.dataSourcePool = dataSourcePool;
this.connection = connection;
this.slowSqlThreshold = slowSqlThreshold;
this.sqlLogging = sqlLogging;
this.jdbcFetchSize = jdbcFetchSize;
}

/**
* @param connection
* the connection
Expand All @@ -68,6 +79,7 @@ public class ConnectionProxy implements Connection {
public ConnectionProxy(Connection connection, long slowSqlThreshold, SqlLoggingType sqlLogging, int jdbcFetchSize) {
super();

this.dataSourcePool = null;
this.connection = connection;
this.slowSqlThreshold = slowSqlThreshold;
this.sqlLogging = sqlLogging;
Expand Down Expand Up @@ -98,7 +110,12 @@ public void clearWarnings() throws SQLException {
*/
@Override
public void close() throws SQLException {
this.connection.close();
if (this.dataSourcePool != null) {
this.dataSourcePool.releaseConnection(this.connection);
}
else {
this.connection.close();
}
}

/**
Expand Down
34 changes: 32 additions & 2 deletions batoo-jdbc/src/main/java/org/batoo/jpa/jdbc/DataSourceProxy.java
Expand Up @@ -46,6 +46,32 @@ public class DataSourceProxy implements DataSource {
private final SqlLoggingType sqlLogging;
private final long slowSqlThreshold;
private final int jdbcFetchSize;
private final boolean externalPoolDS;

/**
* @param datasource
* the custom datasource
* @param external
* if the original datasource is external
* @param slowSqlThreshold
* the time to decide if SQL is deemed as slow
* @param sqlLogging
* the sql logging type
* @param jdbcFetchSize
* the size of the jdbc fetch
*
* @since 2.0.0
*/
public DataSourceProxy(AbstractDataSource datasource, boolean external, SqlLoggingType sqlLogging, long slowSqlThreshold, int jdbcFetchSize) {
super();

this.datasource = datasource;
this.external = external;
this.sqlLogging = sqlLogging;
this.slowSqlThreshold = slowSqlThreshold;
this.jdbcFetchSize = jdbcFetchSize;
this.externalPoolDS = true;
}

/**
* @param datasource
Expand All @@ -69,14 +95,15 @@ public DataSourceProxy(DataSource datasource, boolean external, SqlLoggingType s
this.sqlLogging = sqlLogging;
this.slowSqlThreshold = slowSqlThreshold;
this.jdbcFetchSize = jdbcFetchSize;
this.externalPoolDS = false;
}

/**
* Closes the resource local datasource.
*
*/
public void close() {
if (!this.external) {
if (!this.external && !this.externalPoolDS) {
try {
// close the datasource via reflection
final Method closeMethod = this.datasource.getClass().getMethod("close");
Expand All @@ -88,6 +115,9 @@ public void close() {
DataSourceProxy.LOG.error(e, "Cannot close() the internal datasource");
}
}
else if (this.externalPoolDS) {
((AbstractDataSource) this.datasource).close();
}
}

/**
Expand All @@ -105,7 +135,7 @@ public Connection getConnection() throws SQLException {
*/
@Override
public Connection getConnection(String username, String password) throws SQLException {
return new ConnectionProxy(this.datasource.getConnection(username, password), this.slowSqlThreshold, this.sqlLogging, this.jdbcFetchSize);
throw new UnsupportedOperationException("not supported");
}

/**
Expand Down
10 changes: 10 additions & 0 deletions batoo-jpa-spi/src/main/java/org/batoo/jpa/BJPASettings.java
Expand Up @@ -128,4 +128,14 @@ public interface BJPASettings {
* The name of the sql to execute to import the initial data.
*/
String IMPORT_SQL = "org.batoo.jdbc.import_sql";

/**
* Pluggable DataSource implementation
*/
String DATASOURCE_POOL = "org.batoo.jdbc.datasource.pool";

/**
* Hint for the the pluggable data source
*/
String DATASOURCE_NAME = "org.batoo.jdbc.datasource.name";
}
Expand Up @@ -57,6 +57,7 @@
import org.batoo.jpa.core.impl.deployment.LinkManager;
import org.batoo.jpa.core.impl.deployment.NamedQueriesManager;
import org.batoo.jpa.core.impl.model.MetamodelImpl;
import org.batoo.jpa.jdbc.AbstractDataSource;
import org.batoo.jpa.jdbc.BoneCPDataSource;
import org.batoo.jpa.jdbc.DDLMode;
import org.batoo.jpa.jdbc.DataSourceProxy;
Expand Down Expand Up @@ -109,6 +110,8 @@ public class EntityManagerFactoryImpl implements EntityManagerFactory, Serializa

private boolean open;

private AbstractDataSource dataSourcePool;

/**
* @param name
* the name of the entity manager factory
Expand Down Expand Up @@ -146,7 +149,7 @@ public EntityManagerFactoryImpl(String name, PersistenceParser parser) {
throw new IllegalArgumentException("Illegal value " + this.getProperty(BJPASettings.SQL_LOGGING) + " for " + BJPASettings.SQL_LOGGING);
}

this.dataSource = this.createDatasource(parser);
this.dataSource = this.createDatasource(name, parser);

this.ddlMode = this.readDdlMode();

Expand Down Expand Up @@ -271,7 +274,7 @@ public void close() {
this.open = false;
}

private DataSourceProxy createDatasource(PersistenceParser parser) {
private DataSourceProxy createDatasource(String persistanceUnitName, PersistenceParser parser) {
SqlLoggingType sqlLogging;
long slowSqlThreshold;
int jdbcFetchSize;
Expand Down Expand Up @@ -305,20 +308,30 @@ private DataSourceProxy createDatasource(PersistenceParser parser) {
throw new IllegalArgumentException("Illegal value " + this.getProperty(BJPASettings.FETCH_SIZE) + " for " + BJPASettings.FETCH_SIZE);
}

final boolean external = (parser.getJtaDataSource() != null) || (parser.getNonJtaDataSource() != null);

return new DataSourceProxy(this.createDatasource0(parser), external, sqlLogging, slowSqlThreshold, jdbcFetchSize);
if (this.getProperty(BJPASettings.DATASOURCE_POOL) != null) {
final String poolClassName = (String) this.getProperty(BJPASettings.DATASOURCE_POOL);
final String hintName = (String) this.getProperty(BJPASettings.DATASOURCE_NAME);
try {
final Object newInstance = this.classloader.loadClass(poolClassName).newInstance();
if (newInstance instanceof AbstractDataSource) {
this.dataSourcePool = (AbstractDataSource) newInstance;
}
else {
throw new IllegalArgumentException("Illegal value " + this.getProperty(BJPASettings.DATASOURCE_POOL) + " for "
+ BJPASettings.DATASOURCE_POOL + " Please provide a datasource pool implementation extending org.batoo.jpa.jdbc.AbstractDataSourcePool");
}
}
catch (final Exception e) {
throw new IllegalArgumentException("Class not found: " + this.getProperty(BJPASettings.DATASOURCE_POOL));
}
finally {
this.dataSourcePool.open(persistanceUnitName, hintName);
}
}
return this.createDatasourceProxy(parser, sqlLogging, slowSqlThreshold, jdbcFetchSize);
}

private DataSource createDatasource0(PersistenceParser parser) {
if (parser.getJtaDataSource() != null) {
return parser.getJtaDataSource();
}

if (parser.getNonJtaDataSource() != null) {
return parser.getNonJtaDataSource();
}

try {
// read the properties
final String jdbcDriver = (String) this.getProperty(JPASettings.JDBC_DRIVER);
Expand Down Expand Up @@ -361,6 +374,21 @@ private DataSource createDatasource0(PersistenceParser parser) {
}
}

private DataSourceProxy createDatasourceProxy(PersistenceParser parser, SqlLoggingType sqlLogging, long slowSqlThreshold, int jdbcFetchSize) {
final boolean external = (parser.getJtaDataSource() != null) || (parser.getNonJtaDataSource() != null);
if (parser.getJtaDataSource() != null) {
return new DataSourceProxy(parser.getNonJtaDataSource(), external, sqlLogging, slowSqlThreshold, jdbcFetchSize);
}
if (parser.getNonJtaDataSource() != null) {
return new DataSourceProxy(parser.getJtaDataSource(), external, sqlLogging, slowSqlThreshold, jdbcFetchSize);

This comment has been minimized.

Copy link
@schueffi

schueffi Dec 18, 2012

Here, nonJTA and JTA are mixed.
This should be ...getNonJtaDataSource() ?

}

if (this.dataSourcePool != null) {
return new DataSourceProxy(this.dataSourcePool, external, sqlLogging, slowSqlThreshold, jdbcFetchSize);

This comment has been minimized.

Copy link
@schueffi

schueffi Dec 18, 2012

The same here
This should be ...getJtaDataSource() ?

}
return new DataSourceProxy(this.createDatasource0(parser), external, sqlLogging, slowSqlThreshold, jdbcFetchSize);
}

/**
* {@inheritDoc}
*
Expand Down

0 comments on commit 8826758

Please sign in to comment.