Skip to content

Commit 1d5c0a6

Browse files
committed
better error reporting in DialectContext and service registry
1 parent 110596a commit 1d5c0a6

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ protected <R extends Service> R createService(ServiceBinding<R> serviceBinding)
264264
throw e;
265265
}
266266
catch ( Exception e ) {
267-
throw new ServiceException( "Unable to create requested service [" + serviceBinding.getServiceRole().getName() + "]", e );
267+
throw new ServiceException( "Unable to create requested service ["
268+
+ serviceBinding.getServiceRole().getName() + "] due to: " + e.getMessage(), e );
268269
}
269270
}
270271

hibernate-jcache/src/test/java/org/hibernate/orm/test/jcache/MissingCacheStrategyTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.junit.Rule;
2424
import org.junit.Test;
2525

26-
import org.hamcrest.CoreMatchers;
27-
26+
import static org.hamcrest.CoreMatchers.equalTo;
2827
import static org.hamcrest.CoreMatchers.notNullValue;
2928
import static org.hamcrest.CoreMatchers.nullValue;
29+
import static org.hamcrest.CoreMatchers.startsWith;
3030
import static org.hamcrest.MatcherAssert.assertThat;
3131
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
3232
import static org.junit.Assert.assertTrue;
@@ -65,11 +65,11 @@ public void testMissingCacheStrategyFail() {
6565
}
6666
catch (ServiceException expected) {
6767
assertTyping( CacheException.class, expected.getCause() );
68-
assertThat( expected.getMessage(), CoreMatchers.equalTo( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) );
69-
assertThat( expected.getCause().getMessage(), CoreMatchers.startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) );
68+
assertThat( expected.getMessage(), startsWith( "Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]" ) );
69+
assertThat( expected.getCause().getMessage(), startsWith( "On-the-fly creation of JCache Cache objects is not supported" ) );
7070
}
7171
catch (CacheException expected) {
72-
assertThat( expected.getMessage(), CoreMatchers.equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) );
72+
assertThat( expected.getMessage(), equalTo( "On-the-fly creation of JCache Cache objects is not supported" ) );
7373
}
7474
}
7575

hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectContext.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import java.lang.reflect.Constructor;
1010
import java.sql.Connection;
1111
import java.sql.Driver;
12+
import java.sql.SQLException;
1213
import java.util.Properties;
1314

1415
import org.hibernate.HibernateException;
1516
import org.hibernate.cfg.Environment;
1617
import org.hibernate.dialect.Dialect;
1718
import org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter;
1819
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
20+
import org.hibernate.exception.JDBCConnectionException;
1921
import org.hibernate.internal.util.ReflectHelper;
2022

2123
/**
@@ -27,27 +29,47 @@ public final class DialectContext {
2729

2830
static void init() {
2931
final Properties properties = Environment.getProperties();
32+
final String diverClassName = properties.getProperty( Environment.DRIVER );
3033
final String dialectName = properties.getProperty( Environment.DIALECT );
34+
final String jdbcUrl = properties.getProperty( Environment.URL );
35+
final Properties props = new Properties();
36+
props.setProperty( "user", properties.getProperty( Environment.USER ) );
37+
props.setProperty( "password", properties.getProperty( Environment.PASS ) );
3138
if ( dialectName == null ) {
3239
throw new HibernateException( "The dialect was not set. Set the property hibernate.dialect." );
3340
}
41+
final Constructor<? extends Dialect> constructor;
3442
try {
43+
@SuppressWarnings("unchecked")
3544
final Class<? extends Dialect> dialectClass = ReflectHelper.classForName( dialectName );
36-
final Constructor<? extends Dialect> constructor = dialectClass.getConstructor( DialectResolutionInfo.class );
37-
Driver driver = (Driver) Class.forName( properties.getProperty( Environment.DRIVER ) ).newInstance();
38-
Properties props = new Properties();
39-
props.setProperty( "user", properties.getProperty( Environment.USER ) );
40-
props.setProperty( "password", properties.getProperty( Environment.PASS ) );
41-
try (Connection connection = driver.connect( properties.getProperty( Environment.URL ), props )) {
42-
dialect = constructor.newInstance( new DatabaseMetaDataDialectResolutionInfoAdapter( connection.getMetaData() ) );
43-
}
45+
constructor = dialectClass.getConstructor( DialectResolutionInfo.class );
4446
}
4547
catch (ClassNotFoundException cnfe) {
4648
throw new HibernateException( "Dialect class not found: " + dialectName, cnfe );
4749
}
4850
catch (Exception e) {
4951
throw new HibernateException( "Could not instantiate given dialect class: " + dialectName, e );
5052
}
53+
final Driver driver;
54+
try {
55+
driver = (Driver) Class.forName( diverClassName ).newInstance();
56+
}
57+
catch (ClassNotFoundException cnfe) {
58+
throw new HibernateException( "JDBC Driver class not found: " + dialectName, cnfe );
59+
}
60+
catch (Exception e) {
61+
throw new HibernateException( "Could not instantiate given JDBC driver class: " + dialectName, e );
62+
}
63+
try ( Connection connection = driver.connect( jdbcUrl, props ) ) {
64+
dialect = constructor.newInstance( new DatabaseMetaDataDialectResolutionInfoAdapter( connection.getMetaData() ) );
65+
}
66+
catch (SQLException sqle) {
67+
throw new JDBCConnectionException( "Could not connect to database with JDBC URL: '"
68+
+ jdbcUrl + "' [" + sqle.getMessage() + "]", sqle );
69+
}
70+
catch (Exception e) {
71+
throw new HibernateException( "Could not instantiate given dialect class: " + dialectName, e );
72+
}
5173
}
5274

5375
private DialectContext() {

0 commit comments

Comments
 (0)