|
|
|
@ -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) {
|
|
|
|
|