better error reporting in DialectContext and service registry

This commit is contained in:
Gavin 2022-11-13 13:22:03 +01:00 committed by Gavin King
parent 110596adb7
commit 1d5c0a60d3
3 changed files with 37 additions and 14 deletions

View File

@ -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 );
}
}

View File

@ -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" ) );
}
}

View File

@ -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<? extends Dialect> constructor;
try {
@SuppressWarnings("unchecked")
final Class<? extends Dialect> dialectClass = ReflectHelper.classForName( dialectName );
final Constructor<? extends Dialect> 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() {