Skip to content

some refactoring to DriverManagerConnectionProvider #10691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.internal.util.ValueHolder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;

/**
* Template (as in template pattern) support for {@link ConnectionCreator} implementors.
*
* @author Steve Ebersole
*/
public abstract class BasicConnectionCreator implements ConnectionCreator {
private final ServiceRegistryImplementor serviceRegistry;
private final ServiceRegistry serviceRegistry;

private final String url;
private final Properties connectionProps;
Expand All @@ -35,7 +35,7 @@ public abstract class BasicConnectionCreator implements ConnectionCreator {
private final String initSql;

public BasicConnectionCreator(
ServiceRegistryImplementor serviceRegistry,
ServiceRegistry serviceRegistry,
String url,
Properties connectionProps,
boolean autocommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;
import java.util.Properties;

import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.ServiceRegistry;

/**
* A factory for {@link ConnectionCreator}.
Expand All @@ -19,7 +19,7 @@ public interface ConnectionCreatorFactory {

ConnectionCreator create(
Driver driver,
ServiceRegistryImplementor serviceRegistry,
ServiceRegistry serviceRegistry,
String url,
Properties connectionProps,
Boolean autocommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;
import java.util.Properties;

import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.ServiceRegistry;

/**
* The default factory for {@link ConnectionCreator} instances.
Expand All @@ -25,7 +25,7 @@ private ConnectionCreatorFactoryImpl() {
@Override
public ConnectionCreator create(
Driver driver,
ServiceRegistryImplementor serviceRegistry,
ServiceRegistry serviceRegistry,
String url,
Properties connectionProps,
Boolean autoCommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public DatabaseConnectionInfoImpl(
Class<? extends ConnectionProvider> connectionProviderClass,
String jdbcUrl,
String jdbcDriver,
Class<? extends Dialect> dialectClass, DatabaseVersion dialectVersion,
Class<? extends Dialect> dialectClass,
DatabaseVersion dialectVersion,
boolean hasSchema,
boolean hasCatalog,
String schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.ServiceRegistry;

/**
* A specialized {@link ConnectionCreator} which uses {@link Driver#connect(String, Properties)}
Expand All @@ -22,7 +22,7 @@ public class DriverConnectionCreator extends BasicConnectionCreator {

public DriverConnectionCreator(
Driver driver,
ServiceRegistryImplementor serviceRegistry,
ServiceRegistry serviceRegistry,
String url,
Properties connectionProps,
Boolean autocommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.ServiceRegistry;

/**
* A specialized {@link ConnectionCreator} which uses {@link DriverManager#getConnection(String, Properties)}
Expand All @@ -19,7 +19,7 @@
*/
public class DriverManagerConnectionCreator extends BasicConnectionCreator {
public DriverManagerConnectionCreator(
ServiceRegistryImplementor serviceRegistry,
ServiceRegistry serviceRegistry,
String url,
Properties connectionProps,
Boolean autocommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceException;
Expand All @@ -44,6 +45,7 @@
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInt;
import static org.hibernate.internal.util.config.ConfigurationHelper.getLong;
import static org.hibernate.internal.util.config.ConfigurationHelper.getString;

/**
* A connection provider that uses the {@link DriverManager} directly to open connections and provides
Expand All @@ -70,7 +72,7 @@ public class DriverManagerConnectionProviderImpl

// create the pool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private volatile ServiceRegistryImplementor serviceRegistry;
private volatile ServiceRegistry serviceRegistry;

@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
Expand All @@ -80,67 +82,33 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
@Override
public void configure(Map<String, Object> configurationValues) {
ConnectionInfoLogger.INSTANCE.usingHibernateBuiltInConnectionPool();
PooledConnections pool = buildPool( configurationValues, serviceRegistry );
final PooledConnections pool = buildPool( configurationValues, serviceRegistry );
final long validationInterval = getLong( VALIDATION_INTERVAL, configurationValues, 30 );
state = new PoolState( pool, validationInterval );
}

private PooledConnections buildPool(Map<String,Object> configuration, ServiceRegistryImplementor serviceRegistry) {
final var creator = buildCreator( configuration, serviceRegistry );
private PooledConnections buildPool(Map<String,Object> configuration, ServiceRegistry serviceRegistry) {
// connection settings
final String url = jdbcUrl( configuration );
final String driverClassName = getString( DRIVER, configuration );
final Properties connectionProps = getConnectionProperties( configuration );
final boolean autoCommit = getBoolean( AUTOCOMMIT, configuration ); // default autocommit to false
final Integer isolation = extractIsolation( configuration );
final String initSql = getString( INIT_SQL, configuration );

// pool settings
final int minSize = getInt( MIN_SIZE, configuration, 1 );
final int maxSize = getInt( POOL_SIZE, configuration, 20 );
final int initialSize = getInt( INITIAL_SIZE, configuration, minSize );
return new PooledConnections.Builder( creator )
.autoCommit( autoCommit )
.initialSize( initialSize )
.minSize( minSize )
.maxSize( maxSize )
.validator( this )
.build();
}

private static ConnectionCreator buildCreator(
Map<String,Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
final String url = jdbcUrl( configurationValues );

String driverClassName = (String) configurationValues.get( DRIVER );
boolean success = false;
Driver driver = null;
if ( driverClassName != null ) {
driver = loadDriverIfPossible( driverClassName, serviceRegistry );
success = true;
}
else {
//try to guess the driver class from the JDBC URL
for ( var database: Database.values() ) {
if ( database.matchesUrl( url ) ) {
driverClassName = database.getDriverClassName( url );
if ( driverClassName != null ) {
try {
loadDriverIfPossible( driverClassName, serviceRegistry );
success = true;
}
catch (Exception e) {
//swallow it, since this was not
//an explicit setting by the user
}
break;
}
}
}
final Driver driver = loadDriver( driverClassName, serviceRegistry, url );
if ( driver == null ) {
//we're hoping that the driver is already loaded
logAvailableDrivers();
}

final String driverList = success ? driverClassName : driverList();

final Properties connectionProps = getConnectionProperties( configurationValues );

final boolean autoCommit = getBoolean( AUTOCOMMIT, configurationValues );
final Integer isolation = extractIsolation( configurationValues );
final String initSql = (String) configurationValues.get( INIT_SQL );

final var connectionCreator =
getConnectionCreatorFactory( configurationValues, serviceRegistry )
getConnectionCreatorFactory( configuration, serviceRegistry )
.create(
driver,
serviceRegistry,
Expand All @@ -149,15 +117,14 @@ private static ConnectionCreator buildCreator(
autoCommit,
isolation,
initSql,
configurationValues
configuration
);


try ( var connection = connectionCreator.createConnection() ) {
dbInfo = new DatabaseConnectionInfoImpl(
DriverManagerConnectionProviderImpl.class,
url,
driverList,
driver == null ? null : driver.getClass().getName(),
null,
SimpleDatabaseVersion.ZERO_VERSION,
hasSchema( connection ),
Expand All @@ -168,8 +135,8 @@ private static ConnectionCreator buildCreator(
isolation != null
? toIsolationNiceName( isolation )
: toIsolationNiceName( getIsolation( connection ) ),
getInt( MIN_SIZE, configurationValues, 1 ),
getInt( POOL_SIZE, configurationValues, 20 ),
minSize,
maxSize,
getFetchSize( connection )
);
if ( !connection.getAutoCommit() ) {
Expand All @@ -180,11 +147,39 @@ private static ConnectionCreator buildCreator(
throw new JDBCConnectionException( "Could not create connection", e );
}

return connectionCreator;
return new PooledConnections.Builder( connectionCreator )
.autoCommit( autoCommit )
.initialSize( initialSize )
.minSize( minSize )
.maxSize( maxSize )
.validator( this )
.build();
}

private static Driver loadDriver(String driverClassName, ServiceRegistry serviceRegistry, String url) {
if ( driverClassName != null ) {
return loadDriverIfPossible( driverClassName, serviceRegistry );
}
else {
// try to guess the driver class from the JDBC URL
for ( var database: Database.values() ) {
if ( database.matchesUrl( url ) ) {
final String databaseDriverClassName = database.getDriverClassName( url );
if ( databaseDriverClassName != null ) {
try {
return loadDriverIfPossible( databaseDriverClassName, serviceRegistry );
}
catch (Exception e) {
// swallow it, since this was not an explicit setting by the user
}
}
}
}
return null;
}
}

private static String driverList() {
//we're hoping that the driver is already loaded
private static void logAvailableDrivers() {
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
final var list = new StringBuilder();
DriverManager.drivers()
Expand All @@ -194,20 +189,20 @@ private static String driverList() {
}
list.append( driver.getClass().getName() );
} );
return list.toString();
ConnectionInfoLogger.INSTANCE.availableJdbcDrivers( list.toString() );
}

private static String jdbcUrl(Map<String, Object> configurationValues) {
final String url = (String) configurationValues.get( URL );
private static String jdbcUrl(Map<String, Object> configuration) {
final String url = (String) configuration.get( URL );
if ( url == null ) {
throw new ConnectionProviderConfigurationException( "No JDBC URL specified by property '" + JAKARTA_JDBC_URL + "'" );
}
return url;
}

private static ConnectionCreatorFactory getConnectionCreatorFactory(
Map<String, Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
final Object connectionCreatorFactory = configurationValues.get( CONNECTION_CREATOR_FACTORY );
Map<String, Object> configuration, ServiceRegistry serviceRegistry) {
final Object connectionCreatorFactory = configuration.get( CONNECTION_CREATOR_FACTORY );
final ConnectionCreatorFactory factory;
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
factory = instance;
Expand All @@ -221,7 +216,7 @@ else if ( connectionCreatorFactory != null ) {
return factory == null ? ConnectionCreatorFactoryImpl.INSTANCE : factory;
}

private static Driver loadDriverIfPossible(String driverClassName, ServiceRegistryImplementor serviceRegistry) {
private static Driver loadDriverIfPossible(String driverClassName, ServiceRegistry serviceRegistry) {
if ( driverClassName == null ) {
ConnectionInfoLogger.INSTANCE.debug( "No driver class specified" );
return null;
Expand All @@ -248,7 +243,7 @@ else if ( serviceRegistry != null ) {
}

private static ConnectionCreatorFactory loadConnectionCreatorFactory(
String connectionCreatorFactoryClassName, ServiceRegistryImplementor serviceRegistry) {
String connectionCreatorFactoryClassName, ServiceRegistry serviceRegistry) {
if ( connectionCreatorFactoryClassName == null ) {
ConnectionInfoLogger.INSTANCE.debug( "No connection creator factory class specified" );
return null;
Expand All @@ -261,17 +256,17 @@ else if ( serviceRegistry != null ) {
return factoryClass.newInstance();
}
catch ( Exception e ) {
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded", e );
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName
+ " could not be loaded", e );
}
}
else {
try {
return (ConnectionCreatorFactory) Class.forName( connectionCreatorFactoryClassName ).newInstance();
}
catch (Exception e1) {
throw new ServiceException(
"Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded",
e1 );
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName
+ " could not be loaded", e1 );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public interface ConnectionInfoLogger extends BasicLogger {
+ JdbcSettings.DRIVER + "'")
void jdbcDriverNotSpecified();

@LogMessage(level = INFO)
@Message(id = 10001007, value = "Available JDBC drivers: [%s]")
void availableJdbcDrivers(String availableDrivers);

@LogMessage(level = DEBUG)
@Message(value = "Cleaning up connection pool [%s]", id = 10001008)
void cleaningUpConnectionPool(String info);
Expand Down
Loading