Author: epbernard
Date: 2010-01-08 05:41:10 -0500 (Fri, 08 Jan 2010)
New Revision: 18450
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/beanvalidation/BeanValidationTest.java
Log:
HHH-4675 Bean Validation ConstraintViolationException should trigger a tx rollback
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2010-01-08
06:55:42 UTC (rev 18449)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2010-01-08
10:41:10 UTC (rev 18450)
@@ -56,6 +56,7 @@
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
+import javax.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -444,8 +445,8 @@
catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage() );
}
- catch ( HibernateException he ) {
- throw convert( he );
+ catch ( RuntimeException e ) {
+ throw convert( e );
}
}
@@ -461,8 +462,8 @@
catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage(), e );
}
- catch ( HibernateException he ) {
- throw convert( he );
+ catch ( RuntimeException e ) { //including HibernateException
+ throw convert( e );
}
}
@@ -474,8 +475,8 @@
catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage(), e );
}
- catch ( HibernateException he ) {
- throw convert( he );
+ catch ( RuntimeException e ) { //including HibernateException
+ throw convert( e );
}
}
@@ -553,8 +554,8 @@
//adjustFlushMode();
getSession().flush();
}
- catch ( HibernateException he ) {
- throw convert( he );
+ catch ( RuntimeException e ) {
+ throw convert( e );
}
}
@@ -768,7 +769,7 @@
tx.setRollbackOnly();
}
else {
- //no explicit use of the tx. boudaries methods
+ //no explicit use of the tx. boundaries methods
if ( PersistenceUnitTransactionType.JTA == transactionType ) {
TransactionManager transactionManager =
( ( SessionFactoryImplementor ) getRawSession().getSessionFactory()
).getTransactionManager();
@@ -985,6 +986,7 @@
/**
* {@inheritDoc}
*/
+ //FIXME should we remove all calls to this method and use convert(RuntimeException) ?
public RuntimeException convert(HibernateException e) {
return convert(e, null);
}
@@ -992,6 +994,21 @@
/**
* {@inheritDoc}
*/
+ public RuntimeException convert(RuntimeException e) {
+ RuntimeException result = e;
+ if ( e instanceof HibernateException ) {
+ result = convert( (HibernateException) e );
+ }
+ else if (e instanceof ConstraintViolationException) {
+ markAsRollback();
+ }
+ //if any RT exception should mark the tx for rollback, convert the last else if into a
else
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
if ( e instanceof StaleStateException ) {
PersistenceException converted = wrapStaleStateException( ( StaleStateException ) e
);
Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java 2010-01-08
06:55:42 UTC (rev 18449)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java 2010-01-08
10:41:10 UTC (rev 18450)
@@ -579,7 +579,8 @@
PersistenceException pe = new PersistenceException(
"Unsupported unwrap target type [" + tClass.getName() + "]"
);
- getEntityManager().handlePersistenceException( pe );
+ //It's probably against the spec to not mark the tx for rollback but it will be
easier for people
+ //getEntityManager().handlePersistenceException( pe );
throw pe;
}
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/beanvalidation/BeanValidationTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/beanvalidation/BeanValidationTest.java 2010-01-08
06:55:42 UTC (rev 18449)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/beanvalidation/BeanValidationTest.java 2010-01-08
10:41:10 UTC (rev 18450)
@@ -25,6 +25,10 @@
catch ( ConstraintViolationException e ) {
assertEquals( 1, e.getConstraintViolations().size() );
}
+ assertTrue(
+ "A constraint violation exception should mark the transaction for
rollback",
+ em.getTransaction().getRollbackOnly()
+ );
em.getTransaction().rollback();
em.close();
}
Show replies by date