gpt4 book ai didi

java - 为什么 session bean 方法在抛出 RuntimeException 时抛出 EjbTransactionRolledbackException

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:15 27 4
gpt4 key购买 nike

我正在尝试通过约束验证来持久化实体,当调用 persist - 抛出约束并且调用者得到 EjbTransactionRolledbackException...所以我尝试显式调用验证并抛出 ConstraintViolationException/RuntimeException 并且调用者仍然得到 EjbTransactionRolledbackException...当我抛出 MyException extends Exception - 调用者得到 MyException

即使我显式调用 sc.setRollBackOnly 它仍然发生了 :(

这不应该是行为。

这是怎么回事?

配置:

Netbeans 6.9.1玻璃鱼 3.0.1JPA 2.0 (EclipseLink)EJB 3.1

谢谢!!!

@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);
}

最佳答案

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

提供完整的堆栈跟踪可能会有所帮助。无论如何,我想知道您是如何调用 EJB 的,以及您是否正在传播事务,在这种情况下抛出 EJBTransactionRolledbackException 是系统异常情况下的正确行为。但以下博客文章可能会有所帮助:

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.

资源

关于java - 为什么 session bean 方法在抛出 RuntimeException 时抛出 EjbTransactionRolledbackException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3793649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com