Why session bean method throw EjbTransactionRolledbackException when RuntimeException was thrown

Uri :

I am trying to persist the entity with constraint validation, when invoke persist - there is constraint that thrown and the caller get EjbTransactionRolledbackException... so I try to call the validation explicit and throw ConstraintViolationException/RuntimeException and still the caller get EjbTransactionRolledbackException... when I throw MyException extends Exception - the caller get MyException

Even when I call explicit sc.setRollBackOnly it's still happened :(

This shouldn't be the behavior.

what's going on?

Configuration:

Netbeans 6.9.1 Glassfish 3.0.1 JPA 2.0 (EclipseLink) EJB 3.1

Thanks!!!

@Stateless
public class My {

@PersistenceContext
EntityManager em;

@Resource
Validator  validator;

public Order checkout(Order order) {
    Set<ConstraintViolation<Order>> set = validator.validate(order, Default.class);

    if (!set.isEmpty()) {
        sc.setRollbackOnly();
        //throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(set));
        throw new RuntimeException();
    }

    this.em.persist(order);
}
Pascal Thivent :

so I try to call the validation explicit and throw ConstraintViolationException/RuntimeException and still the caller get EjbTransactionRolledbackException...

Providing the full stacktrace might help. Anyway, I wonder how you are calling your EJB and if you're propagating a transaction, in which case throwing a EJBTransactionRolledbackException is the right behavior in case of a system exception. But the following blog post might help:

Constraint violated, transaction rolled back

When using bean validation on JPA entities within an EJB 3 bean you would actually get an EJBTransactionRolledbackException if there is a constraint violation.

javax.ejb.EJBTransactionRolledbackException: Invalid object at persist time for groups [javax.validation.groups.Default, ]
Caused by: javax.validation.ConstraintViolationException: Invalid object at persist time for groups [javax.validation.groups.Default, ]

This is all nicely according to specification, but not really interesting information. You don't really want to know what happened, you want to know what went wrong.

So I recommend adding the following to your ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
        version="3.0">
   <assembly-descriptor>
      <application-exception>
         <exception-class>javax.validation.ConstraintViolationException</exception-class>
         <rollback>true</rollback>
      </application-exception>
   </assembly-descriptor>
</ejb-jar>

That way you can directly access your violations.

Resources

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Throw a RuntimeException when invoking an unstubbed method

When is the right time to throw a RuntimeException?

RabbitMQ Java client: What happens when a RuntimeException is thrown inside a consumer's handleDelivery() method?

Java expects return value when RuntimeException is thrown

Should methods that throw RuntimeException indicate it in method signature?

'overridden method does not throw Exception' when use lambda with override exception thrown

Why are exceptions thrown from mocks gone when using method count?

Why does the compiler allow throws when the method will never throw the Exception

Is it possible for for a runtimeexception to be thrown when assigning a null reference in Java?

EJB - EJBTransactionRolledbackException argument type mismatch - overloaded method

Why does AppCompatActivity not throw RuntimeException("Stub!") where as Activity does?

Why is the bean 'reactorServiceInstanceLoadBalancer' instantiated just when the method 'getInstance' is called?

Why I obtain this RuntimeException when I try to set a different content view into the onCreate() method of my activity?

Session bean when the application is stateless

What is the expected behavior when a Java 8 Stream throw a RuntimeException?

Why Compensate method doesn't Call when a consumer thrown exception in MassTransit RouterSlip

Why does Google Guava's ArrayListMultimap clear method throw IllegalAccessError when called by method reference?

Exception thrown in async method is not caught - why?

Why is StaleElementReferenceException being thrown in following method?

Why is IllegalArgumentException thrown here by Method.Invoke?

When to use Stateful session bean over Stateless session bean?

Call session scoped bean method on every view

Why transactional does not rollback when RuntimeException occur?

Customizing JAX-RS response when a ConstraintViolationException is thrown by Bean Validation

Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer?

Why does React throw an error when setState method used in a class constructor?

Why does DataFrameGroupBy.boxplot method throw error when given argument "subplots=True/False"?

Why is no TransactionRequiredException thrown when @Transactional is missing?

Why is NoClassDefFoundError thrown when class appears to be in classpath?