Allow setting the dataSource directly on the HikariConfig.

pull/30/head
Brett Wooldridge 11 years ago
parent 137c76e589
commit e35c939ed2

@ -24,6 +24,8 @@ import java.sql.Connection;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -60,6 +62,7 @@ public final class HikariConfig implements HikariConfigMBean
private boolean isJdbc4connectionTest;
private boolean isAutoCommit;
private Properties dataSourceProperties;
private DataSource dataSource;
/**
* Default constructor
@ -212,6 +215,16 @@ public final class HikariConfig implements HikariConfigMBean
}
}
public DataSource getDataSource()
{
return dataSource;
}
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
public String getDataSourceClassName()
{
return dataSourceClassName;
@ -398,6 +411,12 @@ public final class HikariConfig implements HikariConfigMBean
connectionTimeout = CONNECTION_TIMEOUT;
}
if (dataSource == null && dataSourceClassName == null)
{
logger.error("one of either dataSource or dataSourceClassName must be specified");
throw new IllegalStateException("one of either dataSource or dataSourceClassName must be specified");
}
if (idleTimeout < 0)
{
logger.error("idleTimeout cannot be negative.");

@ -82,16 +82,23 @@ public final class HikariPool implements HikariPoolMBean
this.transactionIsolation = configuration.getTransactionIsolation();
this.debug = LOGGER.isDebugEnabled();
String dsClassName = configuration.getDataSourceClassName();
try
if (configuration.getDataSource() == null)
{
Class<?> clazz = this.getClass().getClassLoader().loadClass(dsClassName);
this.dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties());
String dsClassName = configuration.getDataSourceClassName();
try
{
Class<?> clazz = this.getClass().getClassLoader().loadClass(dsClassName);
this.dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties());
}
catch (Exception e)
{
throw new RuntimeException("Could not create datasource instance: " + dsClassName, e);
}
}
catch (Exception e)
else
{
throw new RuntimeException("Could not create datasource instance: " + dsClassName, e);
this.dataSource = configuration.getDataSource();
}
HikariMBeanElf.registerMBeans(configuration, this);

Loading…
Cancel
Save