Relocate initializeDataSource() method.

pull/192/head
Brett Wooldridge 10 years ago
parent 559346dc7c
commit 1143d557d4

@ -147,7 +147,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
this.metricsTracker = MetricsFactory.createMetricsTracker((isRecordMetrics ? configuration.getMetricsTrackerClassName()
: "com.zaxxer.hikari.metrics.MetricsTracker"), this);
this.dataSource = initializeDataSource();
this.dataSource = PoolUtilities.initializeDataSource(configuration.getDataSourceClassName(), configuration.getDataSource(), configuration.getDataSourceProperties(), configuration.getJdbcUrl(), username, password);
if (isRegisteredMbeans) {
HikariMBeanElf.registerMBeans(configuration, this);
@ -554,26 +554,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
assassinExecutor.awaitTermination(5L, TimeUnit.SECONDS);
}
/**
* Create/initialize the underlying DataSource.
*
* @return the DataSource
*/
private DataSource initializeDataSource()
{
String dsClassName = configuration.getDataSourceClassName();
if (configuration.getDataSource() == null && dsClassName != null) {
DataSource dataSource = createInstance(dsClassName, DataSource.class);
PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties());
return dataSource;
}
else if (configuration.getJdbcUrl() != null) {
return new DriverDataSource(configuration.getJdbcUrl(), configuration.getDataSourceProperties(), username, password);
}
return configuration.getDataSource();
}
/**
* Construct the user's connection customizer, if specified.
*

@ -44,7 +44,7 @@ public final class PoolUtilities
* @param start the start time
* @return the elapsed milliseconds
*/
public static long elapsedTimeMs(long start)
public static long elapsedTimeMs(final long start)
{
return System.currentTimeMillis() - start;
}
@ -56,7 +56,7 @@ public final class PoolUtilities
* @param sql the SQL to execute
* @throws SQLException throws if the init SQL execution fails
*/
public static void executeSqlAutoCommit(Connection connection, String sql) throws SQLException
public static void executeSqlAutoCommit(final Connection connection, final String sql) throws SQLException
{
if (sql != null) {
connection.setAutoCommit(true);
@ -70,7 +70,12 @@ public final class PoolUtilities
}
}
public static void quietlySleep(long millis)
/**
* Sleep and transform an InterruptedException into a RuntimeException.
*
* @param millis the number of milliseconds to sleep
*/
public static void quietlySleep(final long millis)
{
try {
Thread.sleep(millis);
@ -107,6 +112,25 @@ public final class PoolUtilities
}
}
/**
* Create/initialize the underlying DataSource.
*
* @return the DataSource
*/
public static DataSource initializeDataSource(String dsClassName, DataSource dataSource, Properties dataSourceProperties, String jdbcUrl, String username, String password)
{
if (dataSource == null && dsClassName != null) {
dataSource = createInstance(dsClassName, DataSource.class);
PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties);
return dataSource;
}
else if (jdbcUrl != null) {
return new DriverDataSource(jdbcUrl, dataSourceProperties, username, password);
}
return dataSource;
}
public static int getTransactionIsolation(String transactionIsolationName)
{
if (transactionIsolationName != null) {
@ -122,6 +146,14 @@ public final class PoolUtilities
return -1;
}
/**
* Create a ThreadPoolExecutor.
*
* @param queueSize the queue size
* @param threadName the thread name
* @param threadFactory an optional ThreadFactory
* @return a ThreadPoolExecutor
*/
public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName, ThreadFactory threadFactory)
{
if (threadFactory == null) {

@ -52,10 +52,8 @@ import com.zaxxer.hikari.proxy.ProxyFactory;
import com.zaxxer.hikari.util.ConcurrentBag;
import com.zaxxer.hikari.util.ConcurrentBag.IBagStateListener;
import com.zaxxer.hikari.util.DefaultThreadFactory;
import com.zaxxer.hikari.util.DriverDataSource;
import com.zaxxer.hikari.util.LeakTask;
import com.zaxxer.hikari.util.PoolUtilities;
import com.zaxxer.hikari.util.PropertyBeanSetter;
/**
* This is the primary connection pool class that provides the basic
@ -146,7 +144,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
this.metricsTracker = MetricsFactory.createMetricsTracker((isRecordMetrics ? configuration.getMetricsTrackerClassName()
: "com.zaxxer.hikari.metrics.MetricsTracker"), this);
this.dataSource = initializeDataSource();
this.dataSource = PoolUtilities.initializeDataSource(configuration.getDataSourceClassName(), configuration.getDataSource(), configuration.getDataSourceProperties(), configuration.getJdbcUrl(), username, password);
if (isRegisteredMbeans) {
HikariMBeanElf.registerMBeans(configuration, this);
@ -533,26 +531,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
assassinExecutor.awaitTermination(5L, TimeUnit.SECONDS);
}
/**
* Create/initialize the underlying DataSource.
*
* @return the DataSource
*/
private DataSource initializeDataSource()
{
String dsClassName = configuration.getDataSourceClassName();
if (configuration.getDataSource() == null && dsClassName != null) {
DataSource dataSource = createInstance(dsClassName, DataSource.class);
PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties());
return dataSource;
}
else if (configuration.getJdbcUrl() != null) {
return new DriverDataSource(configuration.getJdbcUrl(), configuration.getDataSourceProperties(), username, password);
}
return configuration.getDataSource();
}
/**
* Construct the user's connection customizer, if specified.
*

@ -5,14 +5,22 @@ import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
public final class PoolUtilities
{
public static void quietlyCloseConnection(Connection connection)
/**
* Close connection and eat any exception.
*
* @param connection the connection to close
*/
public static void quietlyCloseConnection(final Connection connection)
{
if (connection != null) {
try {
@ -30,7 +38,7 @@ public final class PoolUtilities
* @param start the start time
* @return the elapsed milliseconds
*/
public static long elapsedTimeMs(long start)
public static long elapsedTimeMs(final long start)
{
return System.currentTimeMillis() - start;
}
@ -42,7 +50,7 @@ public final class PoolUtilities
* @param sql the SQL to execute
* @throws SQLException throws if the init SQL execution fails
*/
public static void executeSqlAutoCommit(Connection connection, String sql) throws SQLException
public static void executeSqlAutoCommit(final Connection connection, final String sql) throws SQLException
{
if (sql != null) {
connection.setAutoCommit(true);
@ -52,7 +60,12 @@ public final class PoolUtilities
}
}
public static void quietlySleep(long millis)
/**
* Sleep and transform an InterruptedException into a RuntimeException.
*
* @param millis the number of milliseconds to sleep
*/
public static void quietlySleep(final long millis)
{
try {
Thread.sleep(millis);
@ -89,6 +102,25 @@ public final class PoolUtilities
}
}
/**
* Create/initialize the underlying DataSource.
*
* @return the DataSource
*/
public static DataSource initializeDataSource(String dsClassName, DataSource dataSource, Properties dataSourceProperties, String jdbcUrl, String username, String password)
{
if (dataSource == null && dsClassName != null) {
dataSource = createInstance(dsClassName, DataSource.class);
PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties);
return dataSource;
}
else if (jdbcUrl != null) {
return new DriverDataSource(jdbcUrl, dataSourceProperties, username, password);
}
return dataSource;
}
public static int getTransactionIsolation(String transactionIsolationName)
{
if (transactionIsolationName != null) {
@ -104,6 +136,14 @@ public final class PoolUtilities
return -1;
}
/**
* Create a ThreadPoolExecutor.
*
* @param queueSize the queue size
* @param threadName the thread name
* @param threadFactory an optional ThreadFactory
* @return a ThreadPoolExecutor
*/
public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName, ThreadFactory threadFactory)
{
if (threadFactory == null) {

Loading…
Cancel
Save