Skip to content

Commit ab04163

Browse files
committed
introduce ConnectionProviderConfigurationException
1 parent 6568842 commit ab04163

File tree

8 files changed

+150
-111
lines changed

8 files changed

+150
-111
lines changed

hibernate-agroal/src/main/java/org/hibernate/agroal/internal/AgroalConnectionProvider.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.agroal.internal;
66

7+
import java.io.Serial;
78
import java.sql.Connection;
89
import java.sql.SQLException;
910
import java.sql.DatabaseMetaData;
@@ -19,6 +20,7 @@
1920
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
2021
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
2122
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
23+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
2224
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
2325
import org.hibernate.internal.log.ConnectionInfoLogger;
2426
import org.hibernate.service.UnknownUnwrapTypeException;
@@ -38,10 +40,13 @@
3840
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;
3941

4042
/**
41-
* ConnectionProvider based on Agroal connection pool
42-
* To use this ConnectionProvider set: <pre> hibernate.connection.provider_class AgroalConnectionProvider </pre>
43-
*
44-
* Usual hibernate properties are supported:
43+
* {@link ConnectionProvider} based on Agroal connection pool.
44+
* <p>
45+
* To force the use of this {@code ConnectionProvider} set:
46+
* {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}
47+
* to {@code AgroalConnectionProvider}.
48+
* <p>
49+
* Usual hibernate connection properties are supported:
4550
* <pre>
4651
* hibernate.connection.driver_class
4752
* hibernate.connection.url
@@ -50,8 +55,8 @@
5055
* hibernate.connection.autocommit
5156
* hibernate.connection.isolation
5257
* </pre>
53-
*
54-
* Other configuration options are available, using the {@code hibernate.agroal} prefix
58+
* <p>
59+
* Other configuration options are available, using the {@code hibernate.agroal} prefix.
5560
*
5661
* @see AgroalSettings
5762
* @see AgroalPropertiesReader
@@ -62,6 +67,8 @@
6267
public class AgroalConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
6368

6469
public static final String CONFIG_PREFIX = AGROAL_CONFIG_PREFIX + ".";
70+
71+
@Serial
6572
private static final long serialVersionUID = 1L;
6673
private AgroalDataSource agroalDataSource = null;
6774
private boolean isMetadataAccessAllowed = true;
@@ -113,7 +120,8 @@ public void configure(Map<String, Object> properties) throws HibernateException
113120
}
114121
catch ( Exception e ) {
115122
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
116-
throw new HibernateException( e );
123+
throw new ConnectionProviderConfigurationException(
124+
"Could not configure Agroal: " + e.getMessage(), e );
117125
}
118126
}
119127

hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import com.mchange.v2.c3p0.DataSources;
1616

17-
import org.hibernate.HibernateException;
1817
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
1918
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
2019
import org.hibernate.cfg.C3p0Settings;
@@ -23,6 +22,7 @@
2322
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
2423
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
2524
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
25+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
2626
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
2727
import org.hibernate.internal.log.ConnectionInfoLogger;
2828
import org.hibernate.internal.util.PropertiesHelper;
@@ -141,8 +141,8 @@ public void configure(Map<String, Object> props) {
141141
}
142142
}
143143

144-
Integer minPoolSize = null;
145-
Integer maxPoolSize = null;
144+
final Integer minPoolSize;
145+
final Integer maxPoolSize;
146146
try {
147147

148148
//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
@@ -197,7 +197,8 @@ public void configure(Map<String, Object> props) {
197197
}
198198
catch (Exception e) {
199199
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
200-
throw new HibernateException( e );
200+
throw new ConnectionProviderConfigurationException(
201+
"Could not configure c3p0: " + e.getMessage(), e );
201202
}
202203

203204
isolation = ConnectionProviderInitiator.extractIsolation( props );
@@ -259,17 +260,6 @@ public void stop() {
259260
}
260261
}
261262

262-
/**
263-
* Close the provider.
264-
*
265-
* @deprecated Use {@link #stop} instead
266-
*/
267-
@SuppressWarnings("unused")
268-
@Deprecated
269-
public void close() {
270-
stop();
271-
}
272-
273263
@Override
274264
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
275265
this.serviceRegistry = serviceRegistry;

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.cfg.JdbcSettings;
1515
import org.hibernate.dialect.Dialect;
1616
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
17+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
1718
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
1819
import org.hibernate.engine.jndi.spi.JndiService;
1920
import org.hibernate.internal.log.ConnectionInfoLogger;
@@ -70,7 +71,7 @@ public boolean isUnwrappableAs(Class<?> unwrapType) {
7071
}
7172

7273
@Override
73-
@SuppressWarnings( {"unchecked"})
74+
@SuppressWarnings("unchecked")
7475
public <T> T unwrap(Class<T> unwrapType) {
7576
if ( ConnectionProvider.class.equals( unwrapType )
7677
|| DatasourceConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
@@ -88,26 +89,23 @@ else if ( DataSource.class.isAssignableFrom( unwrapType ) ) {
8889
public void configure(Map<String, Object> configValues) {
8990
if ( dataSource == null ) {
9091
final Object dataSourceSetting = configValues.get( DATASOURCE );
91-
if ( dataSourceSetting instanceof DataSource ds ) {
92-
dataSource = ds;
92+
if ( dataSourceSetting instanceof DataSource instance ) {
93+
dataSource = instance;
9394
}
94-
else {
95-
final String dataSourceJndiName = (String) dataSourceSetting;
96-
if ( dataSourceJndiName == null ) {
97-
throw new HibernateException(
98-
"DataSource to use was not injected nor specified by [" + DATASOURCE
99-
+ "] configuration property"
100-
);
101-
}
102-
this.dataSourceJndiName = dataSourceJndiName;
95+
else if ( dataSourceSetting instanceof String jndiName ) {
96+
dataSourceJndiName = jndiName;
10397
if ( jndiService == null ) {
104-
throw new HibernateException( "Unable to locate JndiService to lookup Datasource" );
98+
throw new ConnectionProviderConfigurationException( "Unable to locate JndiService to lookup Datasource" );
10599
}
106-
dataSource = (DataSource) jndiService.locate( dataSourceJndiName );
100+
dataSource = (DataSource) jndiService.locate( jndiName );
101+
}
102+
else {
103+
throw new ConnectionProviderConfigurationException(
104+
"DataSource to use was not injected nor specified by '" + DATASOURCE + "'" );
107105
}
108106
}
109107
if ( dataSource == null ) {
110-
throw new HibernateException( "Unable to determine appropriate DataSource to use" );
108+
throw new ConnectionProviderConfigurationException( "Unable to determine appropriate DataSource to use" );
111109
}
112110

113111
if ( configValues.containsKey( AvailableSettings.AUTOCOMMIT ) ) {

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.hibernate.dialect.Dialect;
2929
import org.hibernate.dialect.SimpleDatabaseVersion;
3030
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
31+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
3132
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
3233
import org.hibernate.internal.util.config.ConfigurationHelper;
3334
import org.hibernate.service.UnknownUnwrapTypeException;
@@ -90,20 +91,18 @@ private PooledConnections buildPool(Map<String,Object> configurationValues, Serv
9091
final int maxSize = ConfigurationHelper.getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 );
9192
final int initialSize = ConfigurationHelper.getInt( INITIAL_SIZE, configurationValues, minSize );
9293

93-
ConnectionCreator connectionCreator = buildCreator( configurationValues, serviceRegistry );
94-
PooledConnections.Builder pooledConnectionBuilder = new PooledConnections.Builder(
95-
connectionCreator,
96-
autoCommit
97-
);
94+
final ConnectionCreator creator = buildCreator( configurationValues, serviceRegistry );
95+
final PooledConnections.Builder pooledConnectionBuilder = new PooledConnections.Builder( creator, autoCommit );
9896
pooledConnectionBuilder.initialSize( initialSize );
9997
pooledConnectionBuilder.minSize( minSize );
10098
pooledConnectionBuilder.maxSize( maxSize );
10199
pooledConnectionBuilder.validator( this );
102100
return pooledConnectionBuilder.build();
103101
}
104102

105-
private static ConnectionCreator buildCreator(Map<String,Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
106-
final String url = (String) configurationValues.get( AvailableSettings.URL );
103+
private static ConnectionCreator buildCreator(
104+
Map<String,Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
105+
final String url = jdbcUrl( configurationValues );
107106

108107
String driverClassName = (String) configurationValues.get( AvailableSettings.DRIVER );
109108
boolean success = false;
@@ -112,14 +111,14 @@ private static ConnectionCreator buildCreator(Map<String,Object> configurationVa
112111
driver = loadDriverIfPossible( driverClassName, serviceRegistry );
113112
success = true;
114113
}
115-
else if ( url != null ) {
114+
else {
116115
//try to guess the driver class from the JDBC URL
117116
for ( Database database: Database.values() ) {
118117
if ( database.matchesUrl( url ) ) {
119118
driverClassName = database.getDriverClassName( url );
120119
if ( driverClassName != null ) {
121120
try {
122-
loadDriverIfPossible(driverClassName, serviceRegistry);
121+
loadDriverIfPossible( driverClassName, serviceRegistry );
123122
success = true;
124123
}
125124
catch (Exception e) {
@@ -132,49 +131,24 @@ else if ( url != null ) {
132131
}
133132
}
134133

135-
StringBuilder list = new StringBuilder();
136-
if ( !success ) {
137-
//we're hoping that the driver is already loaded
138-
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
139-
Enumeration<Driver> drivers = DriverManager.getDrivers();
140-
while ( drivers.hasMoreElements() ) {
141-
if ( list.length() != 0) {
142-
list.append(", ");
143-
}
144-
list.append( drivers.nextElement().getClass().getName() );
145-
}
146-
}
147-
148-
if ( url == null ) {
149-
throw new HibernateException( "No JDBC URL specified by property " + JAKARTA_JDBC_URL );
150-
}
134+
final String driverList = success ? driverClassName : driverList();
151135

152136
final Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( configurationValues );
153137

154138
final boolean autoCommit = ConfigurationHelper.getBoolean( AvailableSettings.AUTOCOMMIT, configurationValues );
155139
final Integer isolation = ConnectionProviderInitiator.extractIsolation( configurationValues );
156140
final String initSql = (String) configurationValues.get( INIT_SQL );
157141

158-
final Object connectionCreatorFactory = configurationValues.get( CONNECTION_CREATOR_FACTORY );
159-
ConnectionCreatorFactory factory = null;
160-
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory ) {
161-
factory = (ConnectionCreatorFactory) connectionCreatorFactory;
162-
}
163-
else if ( connectionCreatorFactory != null ) {
164-
factory = loadConnectionCreatorFactory( connectionCreatorFactory.toString(), serviceRegistry );
165-
}
166-
if ( factory == null ) {
167-
factory = ConnectionCreatorFactoryImpl.INSTANCE;
168-
}
142+
final ConnectionCreatorFactory factory = getConnectionCreatorFactory( configurationValues, serviceRegistry );
169143

170144
dbInfo = new DatabaseConnectionInfoImpl(
171145
url,
172-
success ? driverClassName : list.toString(),
146+
driverList,
173147
SimpleDatabaseVersion.ZERO_VERSION,
174148
Boolean.toString( autoCommit ),
175-
isolation != null ? ConnectionProviderInitiator.toIsolationNiceName(isolation) : null,
176-
ConfigurationHelper.getInt(MIN_SIZE, configurationValues, 1),
177-
ConfigurationHelper.getInt(AvailableSettings.POOL_SIZE, configurationValues, 20)
149+
isolation != null ? ConnectionProviderInitiator.toIsolationNiceName( isolation ) : null,
150+
ConfigurationHelper.getInt( MIN_SIZE, configurationValues, 1 ),
151+
ConfigurationHelper.getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 )
178152
);
179153

180154
return factory.create(
@@ -189,38 +163,77 @@ else if ( connectionCreatorFactory != null ) {
189163
);
190164
}
191165

166+
private static String driverList() {
167+
//we're hoping that the driver is already loaded
168+
ConnectionInfoLogger.INSTANCE.jdbcDriverNotSpecified();
169+
final StringBuilder list = new StringBuilder();
170+
final Enumeration<Driver> drivers = DriverManager.getDrivers();
171+
while ( drivers.hasMoreElements() ) {
172+
if ( !list.isEmpty() ) {
173+
list.append(", ");
174+
}
175+
list.append( drivers.nextElement().getClass().getName() );
176+
}
177+
return list.toString();
178+
}
179+
180+
private static String jdbcUrl(Map<String, Object> configurationValues) {
181+
final String url = (String) configurationValues.get( AvailableSettings.URL );
182+
if ( url == null ) {
183+
throw new ConnectionProviderConfigurationException( "No JDBC URL specified by property '" + JAKARTA_JDBC_URL + "'" );
184+
}
185+
return url;
186+
}
187+
188+
private static ConnectionCreatorFactory getConnectionCreatorFactory(
189+
Map<String, Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
190+
final Object connectionCreatorFactory = configurationValues.get( CONNECTION_CREATOR_FACTORY );
191+
final ConnectionCreatorFactory factory;
192+
if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
193+
factory = instance;
194+
}
195+
else if ( connectionCreatorFactory != null ) {
196+
factory = loadConnectionCreatorFactory( connectionCreatorFactory.toString(), serviceRegistry );
197+
}
198+
else {
199+
factory = null;
200+
}
201+
return factory == null ? ConnectionCreatorFactoryImpl.INSTANCE : factory;
202+
}
203+
192204
private static Driver loadDriverIfPossible(String driverClassName, ServiceRegistryImplementor serviceRegistry) {
193205
if ( driverClassName == null ) {
194206
ConnectionInfoLogger.INSTANCE.debug( "No driver class specified" );
195207
return null;
196208
}
197-
198-
if ( serviceRegistry != null ) {
199-
final ClassLoaderService classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
200-
final Class<Driver> driverClass = classLoaderService.classForName( driverClassName );
209+
else if ( serviceRegistry != null ) {
210+
final Class<Driver> driverClass =
211+
serviceRegistry.requireService( ClassLoaderService.class )
212+
.classForName( driverClassName );
201213
try {
202214
return driverClass.newInstance();
203215
}
204216
catch ( Exception e ) {
205217
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e );
206218
}
207219
}
208-
209-
try {
210-
return (Driver) Class.forName( driverClassName ).newInstance();
211-
}
212-
catch ( Exception e1 ) {
213-
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
220+
else {
221+
try {
222+
return (Driver) Class.forName( driverClassName ).newInstance();
223+
}
224+
catch (Exception e1) {
225+
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
226+
}
214227
}
215228
}
216229

217-
private static ConnectionCreatorFactory loadConnectionCreatorFactory(String connectionCreatorFactoryClassName, ServiceRegistryImplementor serviceRegistry) {
230+
private static ConnectionCreatorFactory loadConnectionCreatorFactory(
231+
String connectionCreatorFactoryClassName, ServiceRegistryImplementor serviceRegistry) {
218232
if ( connectionCreatorFactoryClassName == null ) {
219233
ConnectionInfoLogger.INSTANCE.debug( "No connection creator factory class specified" );
220234
return null;
221235
}
222-
223-
if ( serviceRegistry != null ) {
236+
else if ( serviceRegistry != null ) {
224237
final ClassLoaderService classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
225238
final Class<ConnectionCreatorFactory> factoryClass =
226239
classLoaderService.classForName( connectionCreatorFactoryClassName );
@@ -231,12 +244,15 @@ private static ConnectionCreatorFactory loadConnectionCreatorFactory(String conn
231244
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded", e );
232245
}
233246
}
234-
235-
try {
236-
return (ConnectionCreatorFactory) Class.forName( connectionCreatorFactoryClassName ).newInstance();
237-
}
238-
catch ( Exception e1 ) {
239-
throw new ServiceException( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded", e1 );
247+
else {
248+
try {
249+
return (ConnectionCreatorFactory) Class.forName( connectionCreatorFactoryClassName ).newInstance();
250+
}
251+
catch (Exception e1) {
252+
throw new ServiceException(
253+
"Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded",
254+
e1 );
255+
}
240256
}
241257
}
242258

0 commit comments

Comments
 (0)