@ -14,7 +14,7 @@
* limitations under the License .
* limitations under the License .
* /
* /
package com.zaxxer.hikari ;
package com.zaxxer.hikari .pool ;
import java.sql.Connection ;
import java.sql.Connection ;
import java.sql.SQLException ;
import java.sql.SQLException ;
@ -32,6 +32,9 @@ import javax.sql.DataSource;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
import com.zaxxer.hikari.HikariConfig ;
import com.zaxxer.hikari.HikariPoolMBean ;
import com.zaxxer.hikari.IConnectionCustomizer ;
import com.zaxxer.hikari.metrics.CodaHaleMetricsTracker ;
import com.zaxxer.hikari.metrics.CodaHaleMetricsTracker ;
import com.zaxxer.hikari.metrics.MetricsTracker ;
import com.zaxxer.hikari.metrics.MetricsTracker ;
import com.zaxxer.hikari.metrics.MetricsTracker.Context ;
import com.zaxxer.hikari.metrics.MetricsTracker.Context ;
@ -53,7 +56,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
{
{
private static final Logger LOGGER = LoggerFactory . getLogger ( HikariPool . class ) ;
private static final Logger LOGGER = LoggerFactory . getLogger ( HikariPool . class ) ;
final DataSource dataSource ;
private final DataSource dataSource ;
private final IConnectionCustomizer connectionCustomizer ;
private final IConnectionCustomizer connectionCustomizer ;
private final HikariConfig configuration ;
private final HikariConfig configuration ;
@ -78,7 +81,12 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
private int transactionIsolation ;
private int transactionIsolation ;
private boolean isDebug ;
private boolean isDebug ;
HikariPool ( HikariConfig configuration )
/ * *
* Construct a HikariPool with the specified configuration .
*
* @param configuration a HikariConfig instance
* /
public HikariPool ( HikariConfig configuration )
{
{
this ( configuration , configuration . getUsername ( ) , configuration . getPassword ( ) ) ;
this ( configuration , configuration . getUsername ( ) , configuration . getPassword ( ) ) ;
}
}
@ -87,8 +95,10 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
* Construct a HikariPool with the specified configuration .
* Construct a HikariPool with the specified configuration .
*
*
* @param configuration a HikariConfig instance
* @param configuration a HikariConfig instance
* @param username authentication username
* @param password authentication password
* /
* /
HikariPool ( HikariConfig configuration , String username , String password )
public HikariPool( HikariConfig configuration , String username , String password )
{
{
this . configuration = configuration ;
this . configuration = configuration ;
this . username = username ;
this . username = username ;
@ -101,7 +111,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
this . lastConnectionFailure = new AtomicReference < Throwable > ( ) ;
this . lastConnectionFailure = new AtomicReference < Throwable > ( ) ;
this . catalog = configuration . getCatalog ( ) ;
this . catalog = configuration . getCatalog ( ) ;
this . connectionCustomizer = configuration. getConnection Customizer( ) ;
this . connectionCustomizer = initialize Customizer( ) ;
this . isAutoCommit = configuration . isAutoCommit ( ) ;
this . isAutoCommit = configuration . isAutoCommit ( ) ;
this . isIsolateInternalQueries = configuration . isIsolateInternalQueries ( ) ;
this . isIsolateInternalQueries = configuration . isIsolateInternalQueries ( ) ;
this . isReadOnly = configuration . isReadOnly ( ) ;
this . isReadOnly = configuration . isReadOnly ( ) ;
@ -137,7 +147,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
* @return a java . sql . Connection instance
* @return a java . sql . Connection instance
* @throws SQLException thrown if a timeout occurs trying to obtain a connection
* @throws SQLException thrown if a timeout occurs trying to obtain a connection
* /
* /
Connection getConnection ( ) throws SQLException
public Connection getConnection ( ) throws SQLException
{
{
final long start = System . currentTimeMillis ( ) ;
final long start = System . currentTimeMillis ( ) ;
final long maxLife = configuration . getMaxLifetime ( ) ;
final long maxLife = configuration . getMaxLifetime ( ) ;
@ -212,7 +222,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
return configuration . getPoolName ( ) ;
return configuration . getPoolName ( ) ;
}
}
void shutdown ( )
public void shutdown ( )
{
{
isShutdown = true ;
isShutdown = true ;
houseKeepingTimer . cancel ( ) ;
houseKeepingTimer . cancel ( ) ;
@ -229,6 +239,38 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
}
}
}
}
public DataSource getDataSource ( )
{
return dataSource ;
}
/ * *
* Permanently close a connection .
*
* @param connectionProxy the connection to actually close
* /
public void closeConnection ( IHikariConnectionProxy connectionProxy )
{
try
{
int tc = totalConnections . decrementAndGet ( ) ;
if ( tc < 0 )
{
LOGGER . warn ( "Internal accounting inconsistency, totalConnections=" + tc , new Exception ( ) ) ;
}
connectionProxy . realClose ( ) ;
}
catch ( SQLException e )
{
return ;
}
finally
{
connectionBag . remove ( connectionProxy ) ;
}
}
// ***********************************************************************
// ***********************************************************************
// IBagStateListener methods
// IBagStateListener methods
// ***********************************************************************
// ***********************************************************************
@ -443,32 +485,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
}
}
}
}
/ * *
* Permanently close a connection .
*
* @param connectionProxy the connection to actually close
* /
void closeConnection ( IHikariConnectionProxy connectionProxy )
{
try
{
int tc = totalConnections . decrementAndGet ( ) ;
if ( tc < 0 )
{
LOGGER . warn ( "Internal accounting inconsistency, totalConnections=" + tc , new Exception ( ) ) ;
}
connectionProxy . realClose ( ) ;
}
catch ( SQLException e )
{
return ;
}
finally
{
connectionBag . remove ( connectionProxy ) ;
}
}
private DataSource initializeDataSource ( )
private DataSource initializeDataSource ( )
{
{
String dsClassName = configuration . getDataSourceClassName ( ) ;
String dsClassName = configuration . getDataSourceClassName ( ) ;
@ -476,8 +492,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
{
{
try
try
{
{
Class < ? > clazz = this . getClass ( ) . getClassLoader ( ) . loadClass ( dsClassName ) ;
DataSource dataSource = PoolUtilities . createInstance ( dsClassName , DataSource . class ) ;
DataSource dataSource = ( DataSource ) clazz . newInstance ( ) ;
PropertyBeanSetter . setTargetFromProperties ( dataSource , configuration . getDataSourceProperties ( ) ) ;
PropertyBeanSetter . setTargetFromProperties ( dataSource , configuration . getDataSourceProperties ( ) ) ;
return dataSource ;
return dataSource ;
}
}
@ -494,6 +509,23 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
return configuration . getDataSource ( ) ;
return configuration . getDataSource ( ) ;
}
}
private IConnectionCustomizer initializeCustomizer ( )
{
if ( configuration . getConnectionCustomizerClassName ( ) ! = null )
{
try
{
return PoolUtilities . createInstance ( configuration . getConnectionCustomizerClassName ( ) , IConnectionCustomizer . class ) ;
}
catch ( Exception e )
{
LOGGER . error ( "Connection customizer could not be created" , e ) ;
}
}
return null ;
}
private void logPoolState ( String . . . prefix )
private void logPoolState ( String . . . prefix )
{
{
int total = totalConnections . get ( ) ;
int total = totalConnections . get ( ) ;