introduce ConnectionProviderConfigurationException
This commit is contained in:
parent
fb94a0be70
commit
8dd1b0daf7
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
package org.hibernate.agroal.internal;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
@ -19,6 +20,7 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
|
||||
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.internal.log.ConnectionInfoLogger;
|
||||
import org.hibernate.service.UnknownUnwrapTypeException;
|
||||
|
@ -38,10 +40,13 @@ import static org.hibernate.cfg.AgroalSettings.AGROAL_CONFIG_PREFIX;
|
|||
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;
|
||||
|
||||
/**
|
||||
* ConnectionProvider based on Agroal connection pool
|
||||
* To use this ConnectionProvider set: <pre> hibernate.connection.provider_class AgroalConnectionProvider </pre>
|
||||
*
|
||||
* Usual hibernate properties are supported:
|
||||
* {@link ConnectionProvider} based on Agroal connection pool.
|
||||
* <p>
|
||||
* To force the use of this {@code ConnectionProvider} set
|
||||
* {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}
|
||||
* to {@code agroal}.
|
||||
* <p>
|
||||
* Usual hibernate connection properties are supported:
|
||||
* <pre>
|
||||
* hibernate.connection.driver_class
|
||||
* hibernate.connection.url
|
||||
|
@ -50,8 +55,8 @@ import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.al
|
|||
* hibernate.connection.autocommit
|
||||
* hibernate.connection.isolation
|
||||
* </pre>
|
||||
*
|
||||
* Other configuration options are available, using the {@code hibernate.agroal} prefix
|
||||
* <p>
|
||||
* Other configuration options are available, using the {@code hibernate.agroal} prefix.
|
||||
*
|
||||
* @see AgroalSettings
|
||||
* @see AgroalPropertiesReader
|
||||
|
@ -62,6 +67,8 @@ import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.al
|
|||
public class AgroalConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
|
||||
|
||||
public static final String CONFIG_PREFIX = AGROAL_CONFIG_PREFIX + ".";
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
private AgroalDataSource agroalDataSource = null;
|
||||
private boolean isMetadataAccessAllowed = true;
|
||||
|
@ -113,7 +120,8 @@ public class AgroalConnectionProvider implements ConnectionProvider, Configurabl
|
|||
}
|
||||
catch ( Exception e ) {
|
||||
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
|
||||
throw new HibernateException( e );
|
||||
throw new ConnectionProviderConfigurationException(
|
||||
"Could not configure Agroal: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ import javax.sql.DataSource;
|
|||
|
||||
import com.mchange.v2.c3p0.DataSources;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||
import org.hibernate.cfg.C3p0Settings;
|
||||
|
@ -23,6 +22,7 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
|
||||
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.internal.log.ConnectionInfoLogger;
|
||||
import org.hibernate.internal.util.PropertiesHelper;
|
||||
|
@ -38,8 +38,13 @@ 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
|
||||
* default if the {@code hibernate.c3p0.*} properties are set.
|
||||
* {@link ConnectionProvider} based on c3p0 connection pool.
|
||||
* <p>
|
||||
* To force the use of this {@code ConnectionProvider} set
|
||||
* {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}
|
||||
* to {@code c3p0}.
|
||||
* <p>
|
||||
* Hibernate selects this by default if the {@code hibernate.c3p0.*} properties are set.
|
||||
*
|
||||
* @author various people
|
||||
* @see ConnectionProvider
|
||||
|
@ -141,8 +146,8 @@ public class C3P0ConnectionProvider
|
|||
}
|
||||
}
|
||||
|
||||
Integer minPoolSize = null;
|
||||
Integer maxPoolSize = null;
|
||||
final Integer minPoolSize;
|
||||
final Integer maxPoolSize;
|
||||
try {
|
||||
|
||||
//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
|
||||
|
@ -197,7 +202,8 @@ public class C3P0ConnectionProvider
|
|||
}
|
||||
catch (Exception e) {
|
||||
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
|
||||
throw new HibernateException( e );
|
||||
throw new ConnectionProviderConfigurationException(
|
||||
"Could not configure c3p0: " + e.getMessage(), e );
|
||||
}
|
||||
|
||||
isolation = ConnectionProviderInitiator.extractIsolation( props );
|
||||
|
@ -259,17 +265,6 @@ public class C3P0ConnectionProvider
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the provider.
|
||||
*
|
||||
* @deprecated Use {@link #stop} instead
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@Deprecated
|
||||
public void close() {
|
||||
stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
|||
public final class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
final SimpleStrategyRegistrationImpl<ConnectionProvider> c3p0 = new SimpleStrategyRegistrationImpl<>(
|
||||
ConnectionProvider.class,
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.hibernate.cfg.AvailableSettings;
|
|||
import org.hibernate.cfg.JdbcSettings;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.engine.jndi.spi.JndiService;
|
||||
import org.hibernate.internal.log.ConnectionInfoLogger;
|
||||
|
@ -70,7 +71,7 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T unwrap(Class<T> unwrapType) {
|
||||
if ( ConnectionProvider.class.equals( unwrapType )
|
||||
|| DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
|
||||
|
@ -88,26 +89,23 @@ public class DatasourceConnectionProviderImpl implements ConnectionProvider, Con
|
|||
public void configure(Map<String, Object> configValues) {
|
||||
if ( dataSource == null ) {
|
||||
final Object dataSourceSetting = configValues.get( DATASOURCE );
|
||||
if ( dataSourceSetting instanceof DataSource ds ) {
|
||||
dataSource = ds;
|
||||
if ( dataSourceSetting instanceof DataSource instance ) {
|
||||
dataSource = instance;
|
||||
}
|
||||
else if ( dataSourceSetting instanceof String jndiName ) {
|
||||
dataSourceJndiName = jndiName;
|
||||
if ( jndiService == null ) {
|
||||
throw new ConnectionProviderConfigurationException( "Unable to locate JndiService to lookup Datasource" );
|
||||
}
|
||||
dataSource = (DataSource) jndiService.locate( jndiName );
|
||||
}
|
||||
else {
|
||||
final String dataSourceJndiName = (String) dataSourceSetting;
|
||||
if ( dataSourceJndiName == null ) {
|
||||
throw new HibernateException(
|
||||
"DataSource to use was not injected nor specified by [" + DATASOURCE
|
||||
+ "] configuration property"
|
||||
);
|
||||
}
|
||||
this.dataSourceJndiName = dataSourceJndiName;
|
||||
if ( jndiService == null ) {
|
||||
throw new HibernateException( "Unable to locate JndiService to lookup Datasource" );
|
||||
}
|
||||
dataSource = (DataSource) jndiService.locate( dataSourceJndiName );
|
||||
throw new ConnectionProviderConfigurationException(
|
||||
"DataSource to use was not injected nor specified by '" + DATASOURCE + "'" );
|
||||
}
|
||||
}
|
||||
if ( dataSource == null ) {
|
||||
throw new HibernateException( "Unable to determine appropriate DataSource to use" );
|
||||
throw new ConnectionProviderConfigurationException( "Unable to determine appropriate DataSource to use" );
|
||||
}
|
||||
|
||||
if ( configValues.containsKey( AvailableSettings.AUTOCOMMIT ) ) {
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.hibernate.dialect.Database;
|
|||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.SimpleDatabaseVersion;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||
import org.hibernate.service.UnknownUnwrapTypeException;
|
||||
|
@ -90,11 +91,8 @@ public class DriverManagerConnectionProviderImpl
|
|||
final int maxSize = ConfigurationHelper.getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 );
|
||||
final int initialSize = ConfigurationHelper.getInt( INITIAL_SIZE, configurationValues, minSize );
|
||||
|
||||
ConnectionCreator connectionCreator = buildCreator( configurationValues, serviceRegistry );
|
||||
PooledConnections.Builder pooledConnectionBuilder = new PooledConnections.Builder(
|
||||
connectionCreator,
|
||||
autoCommit
|
||||
);
|
||||
final ConnectionCreator creator = buildCreator( configurationValues, serviceRegistry );
|
||||
final PooledConnections.Builder pooledConnectionBuilder = new PooledConnections.Builder( creator, autoCommit );
|
||||
pooledConnectionBuilder.initialSize( initialSize );
|
||||
pooledConnectionBuilder.minSize( minSize );
|
||||
pooledConnectionBuilder.maxSize( maxSize );
|
||||
|
@ -102,8 +100,9 @@ public class DriverManagerConnectionProviderImpl
|
|||
return pooledConnectionBuilder.build();
|
||||
}
|
||||
|
||||
private static ConnectionCreator buildCreator(Map<String,Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
|
||||
final String url = (String) configurationValues.get( AvailableSettings.URL );
|
||||
private static ConnectionCreator buildCreator(
|
||||
Map<String,Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
|
||||
final String url = jdbcUrl( configurationValues );
|
||||
|
||||
String driverClassName = (String) configurationValues.get( AvailableSettings.DRIVER );
|
||||
boolean success = false;
|
||||
|
@ -112,14 +111,14 @@ public class DriverManagerConnectionProviderImpl
|
|||
driver = loadDriverIfPossible( driverClassName, serviceRegistry );
|
||||
success = true;
|
||||
}
|
||||
else if ( url != null ) {
|
||||
else {
|
||||
//try to guess the driver class from the JDBC URL
|
||||
for ( Database database: Database.values() ) {
|
||||
if ( database.matchesUrl( url ) ) {
|
||||
driverClassName = database.getDriverClassName( url );
|
||||
if ( driverClassName != null ) {
|
||||
try {
|
||||
loadDriverIfPossible(driverClassName, serviceRegistry);
|
||||
loadDriverIfPossible( driverClassName, serviceRegistry );
|
||||
success = true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
@ -132,22 +131,7 @@ public class DriverManagerConnectionProviderImpl
|
|||
}
|
||||
}
|
||||
|
||||
StringBuilder list = new StringBuilder();
|
||||
if ( !success ) {
|
||||
//we're hoping that the driver is already loaded
|
||||
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
|
||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
while ( drivers.hasMoreElements() ) {
|
||||
if ( list.length() != 0) {
|
||||
list.append(", ");
|
||||
}
|
||||
list.append( drivers.nextElement().getClass().getName() );
|
||||
}
|
||||
}
|
||||
|
||||
if ( url == null ) {
|
||||
throw new HibernateException( "No JDBC URL specified by property " + JAKARTA_JDBC_URL );
|
||||
}
|
||||
final String driverList = success ? driverClassName : driverList();
|
||||
|
||||
final Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( configurationValues );
|
||||
|
||||
|
@ -155,26 +139,16 @@ public class DriverManagerConnectionProviderImpl
|
|||
final Integer isolation = ConnectionProviderInitiator.extractIsolation( configurationValues );
|
||||
final String initSql = (String) configurationValues.get( INIT_SQL );
|
||||
|
||||
final Object connectionCreatorFactory = configurationValues.get( CONNECTION_CREATOR_FACTORY );
|
||||
ConnectionCreatorFactory factory = null;
|
||||
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory ) {
|
||||
factory = (ConnectionCreatorFactory) connectionCreatorFactory;
|
||||
}
|
||||
else if ( connectionCreatorFactory != null ) {
|
||||
factory = loadConnectionCreatorFactory( connectionCreatorFactory.toString(), serviceRegistry );
|
||||
}
|
||||
if ( factory == null ) {
|
||||
factory = ConnectionCreatorFactoryImpl.INSTANCE;
|
||||
}
|
||||
final ConnectionCreatorFactory factory = getConnectionCreatorFactory( configurationValues, serviceRegistry );
|
||||
|
||||
dbInfo = new DatabaseConnectionInfoImpl(
|
||||
url,
|
||||
success ? driverClassName : list.toString(),
|
||||
driverList,
|
||||
SimpleDatabaseVersion.ZERO_VERSION,
|
||||
Boolean.toString( autoCommit ),
|
||||
isolation != null ? ConnectionProviderInitiator.toIsolationNiceName(isolation) : null,
|
||||
ConfigurationHelper.getInt(MIN_SIZE, configurationValues, 1),
|
||||
ConfigurationHelper.getInt(AvailableSettings.POOL_SIZE, configurationValues, 20)
|
||||
isolation != null ? ConnectionProviderInitiator.toIsolationNiceName( isolation ) : null,
|
||||
ConfigurationHelper.getInt( MIN_SIZE, configurationValues, 1 ),
|
||||
ConfigurationHelper.getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 )
|
||||
);
|
||||
|
||||
return factory.create(
|
||||
|
@ -189,15 +163,53 @@ public class DriverManagerConnectionProviderImpl
|
|||
);
|
||||
}
|
||||
|
||||
private static String driverList() {
|
||||
//we're hoping that the driver is already loaded
|
||||
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
|
||||
final StringBuilder list = new StringBuilder();
|
||||
final Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
while ( drivers.hasMoreElements() ) {
|
||||
if ( !list.isEmpty() ) {
|
||||
list.append(", ");
|
||||
}
|
||||
list.append( drivers.nextElement().getClass().getName() );
|
||||
}
|
||||
return list.toString();
|
||||
}
|
||||
|
||||
private static String jdbcUrl(Map<String, Object> configurationValues) {
|
||||
final String url = (String) configurationValues.get( AvailableSettings.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 );
|
||||
final ConnectionCreatorFactory factory;
|
||||
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
|
||||
factory = instance;
|
||||
}
|
||||
else if ( connectionCreatorFactory != null ) {
|
||||
factory = loadConnectionCreatorFactory( connectionCreatorFactory.toString(), serviceRegistry );
|
||||
}
|
||||
else {
|
||||
factory = null;
|
||||
}
|
||||
return factory == null ? ConnectionCreatorFactoryImpl.INSTANCE : factory;
|
||||
}
|
||||
|
||||
private static Driver loadDriverIfPossible(String driverClassName, ServiceRegistryImplementor serviceRegistry) {
|
||||
if ( driverClassName == null ) {
|
||||
ConnectionInfoLogger.INSTANCE.debug( "No driver class specified" );
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( serviceRegistry != null ) {
|
||||
final ClassLoaderService classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
|
||||
final Class<Driver> driverClass = classLoaderService.classForName( driverClassName );
|
||||
else if ( serviceRegistry != null ) {
|
||||
final Class<Driver> driverClass =
|
||||
serviceRegistry.requireService( ClassLoaderService.class )
|
||||
.classForName( driverClassName );
|
||||
try {
|
||||
return driverClass.newInstance();
|
||||
}
|
||||
|
@ -205,22 +217,23 @@ public class DriverManagerConnectionProviderImpl
|
|||
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return (Driver) Class.forName( driverClassName ).newInstance();
|
||||
}
|
||||
catch ( Exception e1 ) {
|
||||
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
|
||||
else {
|
||||
try {
|
||||
return (Driver) Class.forName( driverClassName ).newInstance();
|
||||
}
|
||||
catch (Exception e1) {
|
||||
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ConnectionCreatorFactory loadConnectionCreatorFactory(String connectionCreatorFactoryClassName, ServiceRegistryImplementor serviceRegistry) {
|
||||
private static ConnectionCreatorFactory loadConnectionCreatorFactory(
|
||||
String connectionCreatorFactoryClassName, ServiceRegistryImplementor serviceRegistry) {
|
||||
if ( connectionCreatorFactoryClassName == null ) {
|
||||
ConnectionInfoLogger.INSTANCE.debug( "No connection creator factory class specified" );
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( serviceRegistry != null ) {
|
||||
else if ( serviceRegistry != null ) {
|
||||
final ClassLoaderService classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
|
||||
final Class<ConnectionCreatorFactory> factoryClass =
|
||||
classLoaderService.classForName( connectionCreatorFactoryClassName );
|
||||
|
@ -231,12 +244,15 @@ public class DriverManagerConnectionProviderImpl
|
|||
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded", e );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return (ConnectionCreatorFactory) Class.forName( connectionCreatorFactoryClassName ).newInstance();
|
||||
}
|
||||
catch ( Exception e1 ) {
|
||||
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded", e1 );
|
||||
else {
|
||||
try {
|
||||
return (ConnectionCreatorFactory) Class.forName( connectionCreatorFactoryClassName ).newInstance();
|
||||
}
|
||||
catch (Exception e1) {
|
||||
throw new ServiceException(
|
||||
"Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded",
|
||||
e1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
* Copyright Red Hat Inc. and Hibernate Authors
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.connections.spi;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
|
||||
/**
|
||||
* Occurs when there is a problem configuring a {@link ConnectionProvider}.
|
||||
*
|
||||
* @since 7.0
|
||||
*
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class ConnectionProviderConfigurationException extends HibernateException {
|
||||
public ConnectionProviderConfigurationException(String message) {
|
||||
super( message );
|
||||
}
|
||||
public ConnectionProviderConfigurationException(String message, Throwable cause) {
|
||||
super( message, cause );
|
||||
}
|
||||
}
|
|
@ -66,7 +66,7 @@ public interface ConnectionInfoLogger extends BasicLogger {
|
|||
void unableToDestroyConnectionPool(@Cause Exception e);
|
||||
|
||||
@LogMessage(level = DEBUG)
|
||||
@Message(value = "Could not instantiate connection pool", id = 10001011)
|
||||
@Message(value = "Could not create connection pool", id = 10001011)
|
||||
void unableToInstantiateConnectionPool(@Cause Exception e);
|
||||
|
||||
@LogMessage(level = DEBUG)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
package org.hibernate.hikaricp.internal;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
@ -14,6 +15,7 @@ import org.hibernate.HibernateException;
|
|||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.internal.log.ConnectionInfoLogger;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
|
@ -27,13 +29,18 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;
|
||||
|
||||
/**
|
||||
* HikariCP Connection provider for Hibernate.
|
||||
* {@link ConnectionProvider} based on HikariCP connection pool.
|
||||
* <p>
|
||||
* To force the use of this {@code ConnectionProvider} set
|
||||
* {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}
|
||||
* to {@code hikari} or {@code hikaricp}.
|
||||
*
|
||||
* @author Brett Wooldridge
|
||||
* @author Luca Burgazzoli
|
||||
*/
|
||||
public class HikariCPConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = -9131625057941275711L;
|
||||
private boolean isMetadataAccessAllowed = true;
|
||||
|
||||
|
@ -63,7 +70,8 @@ public class HikariCPConnectionProvider implements ConnectionProvider, Configura
|
|||
}
|
||||
catch (Exception e) {
|
||||
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
|
||||
throw new HibernateException( e );
|
||||
throw new ConnectionProviderConfigurationException(
|
||||
"Could not configure HikariCP: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
|||
public final class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
final SimpleStrategyRegistrationImpl<ConnectionProvider> strategyRegistration = new SimpleStrategyRegistrationImpl<>(
|
||||
ConnectionProvider.class,
|
||||
|
|
|
@ -19,7 +19,7 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
|||
*/
|
||||
public class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
|
||||
private static final List<StrategyRegistration> REGISTRATIONS = Collections.singletonList(
|
||||
(StrategyRegistration) new SimpleStrategyRegistrationImpl<ConnectionProvider>(
|
||||
new SimpleStrategyRegistrationImpl<>(
|
||||
ConnectionProvider.class,
|
||||
UCPConnectionProvider.class,
|
||||
"ucp",
|
||||
|
@ -30,7 +30,6 @@ public class StrategyRegistrationProviderImpl implements StrategyRegistrationPro
|
|||
);
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterable<StrategyRegistration> getStrategyRegistrations() {
|
||||
return REGISTRATIONS;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
package org.hibernate.oracleucp.internal;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
@ -20,6 +21,7 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
|
||||
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
|
||||
import org.hibernate.internal.log.ConnectionInfoLogger;
|
||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||
|
@ -36,32 +38,32 @@ import oracle.ucp.jdbc.PoolDataSourceFactory;
|
|||
|
||||
public class UCPConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
private PoolDataSource ucpDS = null;
|
||||
private UniversalConnectionPoolManager poolManager = null;
|
||||
private static final String UCP_CONFIG_PREFIX = "hibernate.oracleucp";
|
||||
private static final String CONFIG_PREFIX = UCP_CONFIG_PREFIX + ".";
|
||||
private boolean autoCommit;
|
||||
private Integer isolation;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void configure(Map props) throws HibernateException {
|
||||
public void configure(Map<String,Object> props) throws HibernateException {
|
||||
try {
|
||||
ConnectionInfoLogger.INSTANCE.configureConnectionPool( "Ucp" );
|
||||
|
||||
isolation = ConnectionProviderInitiator.extractIsolation( props );
|
||||
autoCommit = ConfigurationHelper.getBoolean( AvailableSettings.AUTOCOMMIT, props );
|
||||
|
||||
UniversalConnectionPoolManager poolManager = UniversalConnectionPoolManagerImpl.
|
||||
getUniversalConnectionPoolManager();
|
||||
UniversalConnectionPoolManager poolManager =
|
||||
UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
|
||||
ucpDS = PoolDataSourceFactory.getPoolDataSource();
|
||||
Properties ucpProps = getConfiguration(props);
|
||||
configureDataSource(ucpDS, ucpProps);
|
||||
}
|
||||
catch (Exception e) {
|
||||
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
|
||||
throw new HibernateException( e );
|
||||
throw new ConnectionProviderConfigurationException(
|
||||
"Could not configure UCP: " + e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,13 +130,11 @@ public class UCPConnectionProvider implements ConnectionProvider, Configurable,
|
|||
copyProperty( AvailableSettings.USER, props, "user", ucpProps );
|
||||
copyProperty( AvailableSettings.PASS, props, "password", ucpProps );
|
||||
|
||||
for ( Object keyo : props.keySet() ) {
|
||||
if ( !(keyo instanceof String) ) {
|
||||
continue;
|
||||
}
|
||||
String key = (String) keyo;
|
||||
if ( key.startsWith( CONFIG_PREFIX ) ) {
|
||||
ucpProps.setProperty( key.substring( CONFIG_PREFIX.length() ), (String) props.get( key ) );
|
||||
for ( Object object : props.keySet() ) {
|
||||
if ( object instanceof String key ) {
|
||||
if ( key.startsWith( CONFIG_PREFIX ) ) {
|
||||
ucpProps.setProperty( key.substring( CONFIG_PREFIX.length() ), (String) props.get( key ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue