为什么在抛出RuntimeException时会话bean方法抛出EjbTransactionRolledbackException [英] Why session bean method throw EjbTransactionRolledbackException when RuntimeException was thrown
问题描述
我正在尝试通过约束验证来保留实体,
当调用持续存在时-存在引发的约束并且调用方获取EjbTransactionRolledbackException
...
因此,我尝试显式调用验证并抛出ConstraintViolationException
/RuntimeException
,而调用方仍然得到EjbTransactionRolledbackException
...
当我抛出MyException
扩展Exception
时-调用方获取MyException
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
即使我叫显式sc.setRollBackOnly
,它仍然会发生:(
Even when I call explicit sc.setRollBackOnly
it's still happened :(
这不应该是这种行为.
怎么回事?
配置:
Netbeans 6.9.1 玻璃鱼3.0.1 JPA 2.0(EclipseLink) EJB 3.1
Netbeans 6.9.1 Glassfish 3.0.1 JPA 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);
}
推荐答案
所以我尝试显式调用验证并抛出ConstraintViolationException/RuntimeException,但调用方仍然会收到EjbTransactionRolledbackException ...
so I try to call the validation explicit and throw ConstraintViolationException/RuntimeException and still the caller get EjbTransactionRolledbackException...
提供完整的堆栈跟踪可能会有所帮助.无论如何,我想知道如何调用EJB以及是否正在传播事务,在这种情况下,抛出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:
违反约束,交易已回滚
在JPA上使用bean验证时 您在EJB 3 bean中的实体 会得到一个 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.
因此,我建议将以下内容添加到 您的ejb-jar.xml:
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.
资源
- 关于EJB和应用程序与系统异常:
- EJB异常处理的最佳实践
- 16.6.异常和交易
- On EJB and application vs system exception:
- Best practices in EJB exception handling
- 16.6. Exceptions and Transactions
这篇关于为什么在抛出RuntimeException时会话bean方法抛出EjbTransactionRolledbackException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Resources