Skip to content

Commit eaa9592

Browse files
committed
introduce ConnectionProviderConfigurationException
1 parent 6568842 commit eaa9592

File tree

10 files changed

+162
-116
lines changed

10 files changed

+162
-116
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 agroal}.
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: 12 additions & 17 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;
@@ -38,8 +38,13 @@
3838
import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger;
3939

4040
/**
41-
* A connection provider that uses a C3P0 connection pool. Hibernate will use this by
42-
* default if the {@code hibernate.c3p0.*} properties are set.
41+
* {@link ConnectionProvider} based on c3p0 connection pool.
42+
* <p>
43+
* To force the use of this {@code ConnectionProvider} set
44+
* {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}
45+
* to {@code c3p0}.
46+
* <p>
47+
* Hibernate selects this by default if the {@code hibernate.c3p0.*} properties are set.
4348
*
4449
* @author various people
4550
* @see ConnectionProvider
@@ -141,8 +146,8 @@ public void configure(Map<String, Object> props) {
141146
}
142147
}
143148

144-
Integer minPoolSize = null;
145-
Integer maxPoolSize = null;
149+
final Integer minPoolSize;
150+
final Integer maxPoolSize;
146151
try {
147152

148153
//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
@@ -197,7 +202,8 @@ public void configure(Map<String, Object> props) {
197202
}
198203
catch (Exception e) {
199204
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e );
200-
throw new HibernateException( e );
205+
throw new ConnectionProviderConfigurationException(
206+
"Could not configure c3p0: " + e.getMessage(), e );
201207
}
202208

203209
isolation = ConnectionProviderInitiator.extractIsolation( props );
@@ -259,17 +265,6 @@ public void stop() {
259265
}
260266
}
261267

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-
273268
@Override
274269
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
275270
this.serviceRegistry = serviceRegistry;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
public final class StrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
2121

2222
@Override
23-
@SuppressWarnings("unchecked")
2423
public Iterable<StrategyRegistration> getStrategyRegistrations() {
2524
final SimpleStrategyRegistrationImpl<ConnectionProvider> c3p0 = new SimpleStrategyRegistrationImpl<>(
2625
ConnectionProvider.class,

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 ) ) {

0 commit comments

Comments
 (0)