From 1d5c0a60d3a83f358aab988a373b64f187fc880e Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 13 Nov 2022 13:22:03 +0100 Subject: [PATCH] better error reporting in DialectContext and service registry --- .../internal/AbstractServiceRegistryImpl.java | 3 +- .../test/jcache/MissingCacheStrategyTest.java | 10 ++--- .../testing/orm/junit/DialectContext.java | 38 +++++++++++++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java index 0db8177a26..78325c17c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java @@ -264,7 +264,8 @@ public abstract class AbstractServiceRegistryImpl throw e; } catch ( Exception e ) { - throw new ServiceException( "Unable to create requested service [" + serviceBinding.getServiceRole().getName() + "]", e ); + throw new ServiceException( "Unable to create requested service [" + + serviceBinding.getServiceRole().getName() + "] due to: " + e.getMessage(), e ); } } diff --git a/hibernate-jcache/src/test/java/org/hibernate/orm/test/jcache/MissingCacheStrategyTest.java b/hibernate-jcache/src/test/java/org/hibernate/orm/test/jcache/MissingCacheStrategyTest.java index df3673de32..5274a48316 100644 --- a/hibernate-jcache/src/test/java/org/hibernate/orm/test/jcache/MissingCacheStrategyTest.java +++ b/hibernate-jcache/src/test/java/org/hibernate/orm/test/jcache/MissingCacheStrategyTest.java @@ -23,10 +23,10 @@ import org.hibernate.testing.logger.Triggerable; import org.junit.Rule; import org.junit.Test; -import org.hamcrest.CoreMatchers; - +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertTrue; @@ -65,11 +65,11 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase { } catch (ServiceException expected) { assertTyping( CacheException.class, expected.getCause() ); - assertThat( expected.getMessage(), CoreMatchers.equalTo( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) ); - assertThat( expected.getCause().getMessage(), CoreMatchers.startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) ); + assertThat( expected.getMessage(), startsWith( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) ); + assertThat( expected.getCause().getMessage(), startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) ); } catch (CacheException expected) { - assertThat( expected.getMessage(), CoreMatchers.equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) ); + assertThat( expected.getMessage(), equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) ); } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectContext.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectContext.java index 997550274c..7a194cf404 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectContext.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectContext.java @@ -9,6 +9,7 @@ package org.hibernate.testing.orm.junit; import java.lang.reflect.Constructor; import java.sql.Connection; import java.sql.Driver; +import java.sql.SQLException; import java.util.Properties; import org.hibernate.HibernateException; @@ -16,6 +17,7 @@ import org.hibernate.cfg.Environment; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter; import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; +import org.hibernate.exception.JDBCConnectionException; import org.hibernate.internal.util.ReflectHelper; /** @@ -27,20 +29,20 @@ public final class DialectContext { static void init() { final Properties properties = Environment.getProperties(); + final String diverClassName = properties.getProperty( Environment.DRIVER ); final String dialectName = properties.getProperty( Environment.DIALECT ); + final String jdbcUrl = properties.getProperty( Environment.URL ); + final Properties props = new Properties(); + props.setProperty( "user", properties.getProperty( Environment.USER ) ); + props.setProperty( "password", properties.getProperty( Environment.PASS ) ); if ( dialectName == null ) { throw new HibernateException( "The dialect was not set. Set the property hibernate.dialect." ); } + final Constructor constructor; try { + @SuppressWarnings("unchecked") final Class dialectClass = ReflectHelper.classForName( dialectName ); - final Constructor constructor = dialectClass.getConstructor( DialectResolutionInfo.class ); - Driver driver = (Driver) Class.forName( properties.getProperty( Environment.DRIVER ) ).newInstance(); - Properties props = new Properties(); - props.setProperty( "user", properties.getProperty( Environment.USER ) ); - props.setProperty( "password", properties.getProperty( Environment.PASS ) ); - try (Connection connection = driver.connect( properties.getProperty( Environment.URL ), props )) { - dialect = constructor.newInstance( new DatabaseMetaDataDialectResolutionInfoAdapter( connection.getMetaData() ) ); - } + constructor = dialectClass.getConstructor( DialectResolutionInfo.class ); } catch (ClassNotFoundException cnfe) { throw new HibernateException( "Dialect class not found: " + dialectName, cnfe ); @@ -48,6 +50,26 @@ public final class DialectContext { catch (Exception e) { throw new HibernateException( "Could not instantiate given dialect class: " + dialectName, e ); } + final Driver driver; + try { + driver = (Driver) Class.forName( diverClassName ).newInstance(); + } + catch (ClassNotFoundException cnfe) { + throw new HibernateException( "JDBC Driver class not found: " + dialectName, cnfe ); + } + catch (Exception e) { + throw new HibernateException( "Could not instantiate given JDBC driver class: " + dialectName, e ); + } + try ( Connection connection = driver.connect( jdbcUrl, props ) ) { + dialect = constructor.newInstance( new DatabaseMetaDataDialectResolutionInfoAdapter( connection.getMetaData() ) ); + } + catch (SQLException sqle) { + throw new JDBCConnectionException( "Could not connect to database with JDBC URL: '" + + jdbcUrl + "' [" + sqle.getMessage() + "]", sqle ); + } + catch (Exception e) { + throw new HibernateException( "Could not instantiate given dialect class: " + dialectName, e ); + } } private DialectContext() {