minor code style improvements to ConnectionProviders

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-06-27 17:45:18 +02:00
parent d0973b28c1
commit c1624dce98
18 changed files with 101 additions and 106 deletions

View File

@ -106,8 +106,8 @@ public class AgroalConnectionProvider implements ConnectionProvider, Configurabl
}
@Override
public void closeConnection(Connection conn) throws SQLException {
conn.close();
public void closeConnection(Connection connection) throws SQLException {
connection.close();
}
@Override

View File

@ -44,10 +44,10 @@ public class PreparedStatementSpyConnectionProvider extends AgroalConnectionProv
}
@Override
public void closeConnection(Connection conn) throws SQLException {
acquiredConnections.remove( conn );
releasedConnections.add( conn );
super.closeConnection( spyContext.getSpiedInstance( conn ) );
public void closeConnection(Connection connection) throws SQLException {
acquiredConnections.remove( connection );
releasedConnections.add( connection );
super.closeConnection( spyContext.getSpiedInstance( connection ) );
}
@Override

View File

@ -33,6 +33,8 @@ import org.hibernate.service.spi.Stoppable;
import static org.hibernate.c3p0.internal.C3P0MessageLogger.C3P0_LOGGER;
import static org.hibernate.c3p0.internal.C3P0MessageLogger.C3P0_MSG_LOGGER;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.extractSetting;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger;
/**
* A connection provider that uses a C3P0 connection pool. Hibernate will use this by
@ -68,33 +70,33 @@ public class C3P0ConnectionProvider
@Override
public Connection getConnection() throws SQLException {
final Connection c = ds.getConnection();
if ( isolation != null && isolation != c.getTransactionIsolation() ) {
c.setTransactionIsolation( isolation );
final Connection connection = ds.getConnection();
if ( isolation != null && isolation != connection.getTransactionIsolation() ) {
connection.setTransactionIsolation( isolation );
}
if ( c.getAutoCommit() != autocommit ) {
c.setAutoCommit( autocommit );
if ( connection.getAutoCommit() != autocommit ) {
connection.setAutoCommit( autocommit );
}
return c;
return connection;
}
@Override
public void closeConnection(Connection conn) throws SQLException {
conn.close();
public void closeConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public boolean isUnwrappableAs(Class<?> unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) ||
C3P0ConnectionProvider.class.isAssignableFrom( unwrapType ) ||
DataSource.class.isAssignableFrom( unwrapType );
return ConnectionProvider.class.equals( unwrapType )
|| C3P0ConnectionProvider.class.isAssignableFrom( unwrapType )
|| DataSource.class.isAssignableFrom( unwrapType );
}
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> unwrapType) {
if ( ConnectionProvider.class.equals( unwrapType ) ||
C3P0ConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
if ( ConnectionProvider.class.equals( unwrapType )
|| C3P0ConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
return (T) this;
}
else if ( DataSource.class.isAssignableFrom( unwrapType ) ) {
@ -124,7 +126,7 @@ public class C3P0ConnectionProvider
C3P0_MSG_LOGGER.c3p0UsingDriver( jdbcDriverClass, jdbcUrl );
C3P0_MSG_LOGGER.connectionProperties( ConfigurationHelper.maskOut( connectionProps, "password" ) );
autocommit = ConfigurationHelper.getBoolean( JdbcSettings.AUTOCOMMIT, props );
autocommit = getBoolean( JdbcSettings.AUTOCOMMIT, props );
C3P0_MSG_LOGGER.autoCommitMode( autocommit );
if ( jdbcDriverClass == null ) {
@ -142,12 +144,12 @@ public class C3P0ConnectionProvider
try {
//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
final Integer minPoolSize = ConfigurationHelper.getInteger( C3p0Settings.C3P0_MIN_SIZE, props );
final Integer maxPoolSize = ConfigurationHelper.getInteger( C3p0Settings.C3P0_MAX_SIZE, props );
final Integer maxIdleTime = ConfigurationHelper.getInteger( C3p0Settings.C3P0_TIMEOUT, props );
final Integer maxStatements = ConfigurationHelper.getInteger( C3p0Settings.C3P0_MAX_STATEMENTS, props );
final Integer acquireIncrement = ConfigurationHelper.getInteger( C3p0Settings.C3P0_ACQUIRE_INCREMENT, props );
final Integer idleTestPeriod = ConfigurationHelper.getInteger( C3p0Settings.C3P0_IDLE_TEST_PERIOD, props );
final Integer minPoolSize = getInteger( C3p0Settings.C3P0_MIN_SIZE, props );
final Integer maxPoolSize = getInteger( C3p0Settings.C3P0_MAX_SIZE, props );
final Integer maxIdleTime = getInteger( C3p0Settings.C3P0_TIMEOUT, props );
final Integer maxStatements = getInteger( C3p0Settings.C3P0_MAX_STATEMENTS, props );
final Integer acquireIncrement = getInteger( C3p0Settings.C3P0_ACQUIRE_INCREMENT, props );
final Integer idleTestPeriod = getInteger( C3p0Settings.C3P0_IDLE_TEST_PERIOD, props );
final Properties c3props = new Properties();
@ -178,7 +180,7 @@ public class C3P0ConnectionProvider
// revert to traditional hibernate behavior of setting initialPoolSize to minPoolSize
// unless otherwise specified with a c3p0.*-style parameter.
final Integer initialPoolSize = ConfigurationHelper.getInteger( C3P0_STYLE_INITIAL_POOL_SIZE, props );
final Integer initialPoolSize = getInteger( C3P0_STYLE_INITIAL_POOL_SIZE, props );
if ( initialPoolSize == null ) {
setOverwriteProperty( "", C3P0_STYLE_INITIAL_POOL_SIZE, props, c3props, minPoolSize );
}

View File

@ -49,10 +49,9 @@ public class C3P0ProxyConnectionProvider extends C3P0ConnectionProvider {
}
@Override
public void closeConnection(Connection conn) throws SQLException {
Connection originalConnection = connectionSpyMap.get( conn );
super.closeConnection( originalConnection != null ? originalConnection : conn );
public void closeConnection(Connection connection) throws SQLException {
final Connection originalConnection = connectionSpyMap.get( connection );
super.closeConnection( originalConnection != null ? originalConnection : connection);
}
public Map<Connection, Connection> getConnectionSpyMap() {

View File

@ -57,16 +57,16 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con
@Override
public boolean isUnwrappableAs(Class<?> unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) ||
DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ||
DataSource.class.isAssignableFrom( unwrapType );
return ConnectionProvider.class.equals( unwrapType )
|| DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType )
|| DataSource.class.isAssignableFrom( unwrapType );
}
@Override
@SuppressWarnings( {"unchecked"})
public <T> T unwrap(Class<T> unwrapType) {
if ( ConnectionProvider.class.equals( unwrapType ) ||
DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
if ( ConnectionProvider.class.equals( unwrapType )
|| DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
return (T) this;
}
else if ( DataSource.class.isAssignableFrom( unwrapType ) ) {
@ -79,13 +79,13 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con
@Override
public void configure(Map<String, Object> configValues) {
if ( this.dataSource == null ) {
final Object dataSource = configValues.get( Environment.DATASOURCE );
if ( dataSource instanceof DataSource ) {
this.dataSource = (DataSource) dataSource;
if ( dataSource == null ) {
final Object dataSourceSetting = configValues.get( Environment.DATASOURCE );
if ( dataSourceSetting instanceof DataSource ) {
dataSource = (DataSource) dataSourceSetting;
}
else {
final String dataSourceJndiName = (String) dataSource;
final String dataSourceJndiName = (String) dataSourceSetting;
if ( dataSourceJndiName == null ) {
throw new HibernateException(
"DataSource to use was not injected nor specified by [" + Environment.DATASOURCE
@ -95,10 +95,10 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con
if ( jndiService == null ) {
throw new HibernateException( "Unable to locate JndiService to lookup Datasource" );
}
this.dataSource = (DataSource) jndiService.locate( dataSourceJndiName );
dataSource = (DataSource) jndiService.locate( dataSourceJndiName );
}
}
if ( this.dataSource == null ) {
if ( dataSource == null ) {
throw new HibernateException( "Unable to determine appropriate DataSource to use" );
}

View File

@ -263,11 +263,11 @@ public class DriverManagerConnectionProviderImpl
}
@Override
public void closeConnection(Connection conn) throws SQLException {
public void closeConnection(Connection connection) throws SQLException {
if ( state == null ) {
throw new IllegalStateException( "Cannot close a connection as the driver manager is not properly initialized" );
}
state.closeConnection( conn );
state.closeConnection( connection );
}
@Override

View File

@ -45,7 +45,7 @@ public class UserSuppliedConnectionProviderImpl implements ConnectionProvider {
}
@Override
public void closeConnection(Connection conn) throws SQLException {
public void closeConnection(Connection connection) throws SQLException {
throw new UnsupportedOperationException( "The application must supply JDBC connections" );
}

View File

@ -45,12 +45,12 @@ public interface ConnectionProvider extends Service, Wrapped {
/**
* Release a connection from Hibernate use.
*
* @param conn The JDBC connection to release
* @param connection The JDBC connection to release
*
* @throws SQLException Indicates a problem closing the connection
* @throws org.hibernate.HibernateException Indicates a problem otherwise releasing a connection.
*/
void closeConnection(Connection conn) throws SQLException;
void closeConnection(Connection connection) throws SQLException;
/**
* Does this connection provider support aggressive release of JDBC connections and later

View File

@ -111,7 +111,7 @@ public class QualifiedTableNamingTest extends BaseNonConfigCoreFunctionalTestCas
}
@Override
public void closeConnection(Connection conn) throws SQLException {
public void closeConnection(Connection connection) {
}
@Override

View File

@ -47,8 +47,7 @@ public class ExplicitConnectionProviderInstanceTest extends BaseUnitTestCase {
}
@Override
public void closeConnection(Connection conn) throws SQLException {
public void closeConnection(Connection connection) {
}
@Override

View File

@ -140,8 +140,9 @@ public class TransactionCommitFailureTest {
}
@Override
public void closeConnection(Connection conn) throws SQLException {
final ConnectionInvocationHandler handler = (ConnectionInvocationHandler) Proxy.getInvocationHandler( conn );
public void closeConnection(Connection connection) throws SQLException {
final ConnectionInvocationHandler handler = (ConnectionInvocationHandler)
Proxy.getInvocationHandler( connection );
super.closeConnection( handler.delegate );
connectionIsOpen.set( false );
}

View File

@ -71,17 +71,12 @@ public class HikariCPConnectionProvider implements ConnectionProvider, Configura
@Override
public Connection getConnection() throws SQLException {
Connection conn = null;
if ( hds != null ) {
conn = hds.getConnection();
}
return conn;
return hds != null ? hds.getConnection() : null;
}
@Override
public void closeConnection(Connection conn) throws SQLException {
conn.close();
public void closeConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
@ -99,8 +94,8 @@ public class HikariCPConnectionProvider implements ConnectionProvider, Configura
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> unwrapType) {
if ( ConnectionProvider.class.equals( unwrapType ) ||
HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
if ( ConnectionProvider.class.equals( unwrapType )
|| HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
return (T) this;
}
else if ( DataSource.class.isAssignableFrom( unwrapType ) ) {
@ -117,7 +112,6 @@ public class HikariCPConnectionProvider implements ConnectionProvider, Configura
@Override
public void stop() {
HikariDataSource hds = this.hds;
if ( hds != null ) {
hds.close();
}

View File

@ -46,10 +46,10 @@ public class PreparedStatementSpyConnectionProvider
}
@Override
public void closeConnection(Connection conn) throws SQLException {
acquiredConnections.remove( conn );
releasedConnections.add( conn );
super.closeConnection( spyContext.getSpiedInstance( conn ) );
public void closeConnection(Connection connection) throws SQLException {
acquiredConnections.remove( connection );
releasedConnections.add( connection );
super.closeConnection( spyContext.getSpiedInstance( connection ) );
}
@Override

View File

@ -21,8 +21,6 @@ import org.hibernate.cfg.JdbcSettings;
import org.hibernate.cfg.ProxoolSettings;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
@ -34,6 +32,8 @@ import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.proxool.internal.ProxoolMessageLogger.PROXOOL_LOGGER;
import static org.hibernate.proxool.internal.ProxoolMessageLogger.PROXOOL_MESSAGE_LOGGER;
@ -63,34 +63,34 @@ public class ProxoolConnectionProvider
@Override
public Connection getConnection() throws SQLException {
// get a connection from the pool (thru DriverManager, cfr. Proxool doc)
final Connection c = DriverManager.getConnection( proxoolAlias );
// get a connection from the pool (through DriverManager, cfr. Proxool doc)
final Connection connection = DriverManager.getConnection( proxoolAlias );
// set the Transaction Isolation if defined
if ( isolation != null ) {
c.setTransactionIsolation( isolation );
connection.setTransactionIsolation( isolation );
}
// toggle autoCommit to false if set
if ( c.getAutoCommit() != autocommit ) {
c.setAutoCommit( autocommit );
if ( connection.getAutoCommit() != autocommit ) {
connection.setAutoCommit( autocommit );
}
// return the connection
return c;
return connection;
}
@Override
public boolean isUnwrappableAs(Class<?> unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) ||
ProxoolConnectionProvider.class.isAssignableFrom( unwrapType );
return ConnectionProvider.class.equals( unwrapType )
|| ProxoolConnectionProvider.class.isAssignableFrom( unwrapType );
}
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> unwrapType) {
if ( ConnectionProvider.class.equals( unwrapType ) ||
ProxoolConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
if ( ConnectionProvider.class.equals( unwrapType )
|| ProxoolConnectionProvider.class.isAssignableFrom( unwrapType ) ) {
return (T) this;
}
else {
@ -99,13 +99,13 @@ public class ProxoolConnectionProvider
}
@Override
public void closeConnection(Connection conn) throws SQLException {
conn.close();
public void closeConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
}
@Override
@ -122,7 +122,7 @@ public class ProxoolConnectionProvider
// already has Proxool pools running, and this provider is to just borrow one of these
if ( "true".equals( externalConfig ) ) {
// Validate that an alias name was provided to determine which pool to use
if ( !StringHelper.isNotEmpty( proxoolAlias ) ) {
if ( !isNotEmpty( proxoolAlias ) ) {
final String msg = PROXOOL_MESSAGE_LOGGER.unableToConfigureProxoolProviderToUseExistingInMemoryPool( ProxoolSettings.PROXOOL_POOL_ALIAS );
PROXOOL_LOGGER.error( msg );
throw new HibernateException( msg );
@ -137,11 +137,11 @@ public class ProxoolConnectionProvider
// Configured using the JAXP Configurator
}
else if ( StringHelper.isNotEmpty( jaxpFile ) ) {
else if ( isNotEmpty( jaxpFile ) ) {
PROXOOL_MESSAGE_LOGGER.configuringProxoolProviderUsingJaxpConfigurator( jaxpFile );
// Validate that an alias name was provided to determine which pool to use
if ( !StringHelper.isNotEmpty( proxoolAlias ) ) {
if ( !isNotEmpty( proxoolAlias ) ) {
final String msg = PROXOOL_MESSAGE_LOGGER.unableToConfigureProxoolProviderToUseJaxp( ProxoolSettings.PROXOOL_POOL_ALIAS );
PROXOOL_LOGGER.error( msg );
throw new HibernateException( msg );
@ -162,11 +162,11 @@ public class ProxoolConnectionProvider
// Configured using the Properties File Configurator
}
else if ( StringHelper.isNotEmpty( propFile ) ) {
else if ( isNotEmpty( propFile ) ) {
PROXOOL_MESSAGE_LOGGER.configuringProxoolProviderUsingPropertiesFile( propFile );
// Validate that an alias name was provided to determine which pool to use
if ( !StringHelper.isNotEmpty( proxoolAlias ) ) {
if ( !isNotEmpty( proxoolAlias ) ) {
final String msg = PROXOOL_MESSAGE_LOGGER.unableToConfigureProxoolProviderToUsePropertiesFile( ProxoolSettings.PROXOOL_POOL_ALIAS );
PROXOOL_LOGGER.error( msg );
throw new HibernateException( msg );
@ -190,7 +190,7 @@ public class ProxoolConnectionProvider
isolation = ConnectionProviderInitiator.extractIsolation( props );
PROXOOL_MESSAGE_LOGGER.jdbcIsolationLevel( ConnectionProviderInitiator.toIsolationNiceName( isolation ) );
autocommit = ConfigurationHelper.getBoolean( JdbcSettings.AUTOCOMMIT, props );
autocommit = getBoolean( JdbcSettings.AUTOCOMMIT, props );
PROXOOL_MESSAGE_LOGGER.autoCommitMode( autocommit );
}

View File

@ -91,8 +91,8 @@ public class ConnectionProviderDelegate implements
}
@Override
public void closeConnection(Connection conn) throws SQLException {
connectionProvider.closeConnection( conn );
public void closeConnection(Connection connection) throws SQLException {
connectionProvider.closeConnection( connection );
}
@Override

View File

@ -157,14 +157,14 @@ public class JtaAwareConnectionProviderImpl implements ConnectionProvider, Confi
}
@Override
public void closeConnection(Connection conn) throws SQLException {
if ( conn == null ) {
public void closeConnection(Connection connection) throws SQLException {
if ( connection == null ) {
return;
}
if ( nonEnlistedConnections.contains( conn ) ) {
nonEnlistedConnections.remove( conn );
delegate.closeConnection( conn );
if ( nonEnlistedConnections.contains( connection ) ) {
nonEnlistedConnections.remove( connection );
delegate.closeConnection( connection );
}
// else {

View File

@ -78,10 +78,10 @@ public class PreparedStatementSpyConnectionProvider extends ConnectionProviderDe
}
@Override
public void closeConnection(Connection conn) throws SQLException {
acquiredConnections.remove( conn );
releasedConnections.add( conn );
super.closeConnection( spyContext.getSpiedInstance( conn ) );
public void closeConnection(Connection connection) throws SQLException {
acquiredConnections.remove( connection );
releasedConnections.add( connection );
super.closeConnection( spyContext.getSpiedInstance( connection ) );
}
@Override

View File

@ -62,8 +62,8 @@ public class ViburDBCPConnectionProvider implements ConnectionProvider, Configur
}
@Override
public void closeConnection(Connection conn) throws SQLException {
conn.close();
public void closeConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
@ -81,8 +81,8 @@ public class ViburDBCPConnectionProvider implements ConnectionProvider, Configur
@Override
public boolean isUnwrappableAs(Class<?> unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) ||
ViburDBCPConnectionProvider.class.isAssignableFrom( unwrapType );
return ConnectionProvider.class.equals( unwrapType )
|| ViburDBCPConnectionProvider.class.isAssignableFrom( unwrapType );
}
@Override