Reliably expose nested cause exception message for PersistenceException

Issue: SPR-16559
This commit is contained in:
Juergen Hoeller
2018-03-06 23:06:14 +01:00
parent 90d768bb7f
commit eb9c43dcbc
3 changed files with 50 additions and 18 deletions

View File

@@ -425,8 +425,13 @@ public class LocalSessionFactoryBuilder extends Configuration {
throw new IllegalStateException("Interrupted during initialization of Hibernate SessionFactory", ex);
}
catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof HibernateException) {
// Rethrow a provider configuration exception (possibly with a nested cause) directly
throw (HibernateException) cause;
}
throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " +
ex.getMessage(), ex.getCause());
ex.getMessage(), cause);
}
}
}

View File

@@ -385,11 +385,32 @@ public abstract class AbstractEntityManagerFactoryBean implements
}
private EntityManagerFactory buildNativeEntityManagerFactory() {
EntityManagerFactory emf = createNativeEntityManagerFactory();
EntityManagerFactory emf;
try {
emf = createNativeEntityManagerFactory();
}
catch (PersistenceException ex) {
if (ex.getClass() == PersistenceException.class) {
// Plain PersistenceException wrapper for underlying exception?
// Make sure the nested exception message is properly exposed,
// along the lines of Spring's NestedRuntimeException.getMessage()
Throwable cause = ex.getCause();
if (cause != null) {
String message = ex.getMessage();
String causeString = cause.toString();
if (!message.endsWith(causeString)) {
throw new PersistenceException(message + "; nested exception is " + causeString, cause);
}
}
}
throw ex;
}
JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
if (jpaVendorAdapter != null) {
jpaVendorAdapter.postProcessEntityManagerFactory(emf);
}
if (logger.isInfoEnabled()) {
logger.info("Initialized JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'");
}
@@ -416,6 +437,7 @@ public abstract class AbstractEntityManagerFactoryBean implements
ifcs.add(EntityManagerFactory.class);
}
ifcs.add(EntityManagerFactoryInfo.class);
try {
return (EntityManagerFactory) Proxy.newProxyInstance(this.beanClassLoader,
ClassUtils.toClassArray(ifcs), new ManagedEntityManagerFactoryInvocationHandler(this));
@@ -521,8 +543,13 @@ public abstract class AbstractEntityManagerFactoryBean implements
throw new IllegalStateException("Interrupted during initialization of native EntityManagerFactory", ex);
}
catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof PersistenceException) {
// Rethrow a provider configuration exception (possibly with a nested cause) directly
throw (PersistenceException) cause;
}
throw new IllegalStateException("Failed to asynchronously initialize native EntityManagerFactory: " +
ex.getMessage(), ex.getCause());
ex.getMessage(), cause);
}
}
}