Pourquoi méthode haricot séance jeter EjbTransactionRolledbackException quand RuntimeException a été jeté

StackOverflow https://stackoverflow.com/questions/3793649

Question

J'essaie de conserver l'entité avec validation de contrainte, quand Invoke persistent - il y a contrainte jeté et l'appelant get EjbTransactionRolledbackException ... donc je tente d'appeler la validation ConstraintViolationException explicite et lancer / RuntimeException et encore l'appelant get EjbTransactionRolledbackException ... quand je jette MyException étend Exception - l'appelant se MyException

Même quand je l'appelle sc.setRollBackOnly explicite, il est toujours arrivé: (

Cela ne devrait pas être le comportement.

ce qui se passe?

Configuration:

Netbeans 6.9.1 GlassFish 3.0.1 JPA 2.0 (EclipseLink) EJB 3.1

Merci !!!

@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);
}
Était-ce utile?

La solution

  

donc je tente d'appeler la validation explicite et jeter ConstraintViolationException / RuntimeException et encore l'appelant se EjbTransactionRolledbackException ...

Fournir la pleine stacktrace pourrait aider. Quoi qu'il en soit, je me demande comment vous appelez votre EJB et si vous propager une transaction, auquel cas jeter un EJBTransactionRolledbackException est le bon comportement en cas d'exception du système. Mais le message de blog suivants peuvent vous aider:

  

Constraint violé, transaction a été annulée

     

Lors de l'utilisation validation de haricot sur JPA   entités au sein d'un bean EJB 3 vous   serait en fait obtenir un   EJBTransactionRolledbackException si   il y a une violation de contrainte.

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, ]
     

Ceci est tout à bien selon   cahier des charges, mais pas vraiment   Une information intéressante. Vous ne faites pas   vraiment envie de savoir ce qui est arrivé, vous   veulent savoir ce qui a mal tourné.

     

Je recommande donc d'ajouter ce qui suit à   votre 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>
     

De cette façon, vous pouvez accéder directement à votre   violations.

Ressources

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top