2

我正在尝试使用以下组件开始使用简单的 java ee 应用程序:JSF 2.0、JPA EclipseLink、Glasshfish 3。

这是一些片段,支持bean:

@Inject
private ProductsFacade model;    
public void saveRow(Products p) {
        model.edit(p);
}

产品外观:

@Stateless
public class ProductsFacade extends AbstractFacade<Products> {
    @PersistenceContext
    private EntityManager em;
    public void edit(Products entity) {
        em.merge(entity);
    }
    ....

Products 是一个带有 bean 验证注解的实体 bean。

现在,当用户错误填写表单时,'model.edit' 会抛出 EjbException,我用 catch 处理它,所以saveRow支持 bean 方法现在看起来不那么简洁了:

public void saveRow(Products p) {
    try {
        model.edit(p);
    } catch (EJBException e) {
        if(e.getCause().getClass().getName().equals("javax.validation.ConstraintViolationException")) {
            handleConstraintViolation((ConstraintViolationException)e.getCause());
        }
    }
}

并且仍然 glassfish 日志充满了“警告:javax.ejb.EJBException”和长痕迹。我有一些疑问:

  1. 我的设置有多正确?我知道 jsf 应该处理 BeanValidation,但在我的情况下它没有。
  2. 如何禁用EJBException警告,使服务器日志不会被污染
  3. 有没有更好的方法来处理 EjbException ?
4

1 回答 1

1

EJBExceptions 触发当前 JTA 事务的回滚,无论你是否捕获它们。对启动事务的调用ProductsFacade#edit()(除非将一个事务传播给它,这里似乎不是这种情况),因为它是“从外部”对 SessionBean 的调用。如果您不希望您的事务在这些情况下回滚,则必须在将损坏EntityEntityManager.

There are several quirks and stuff to do here to avoid this situation. For example you can have the ProductsFacade handle transactions: @TransactionManagement(TransactionManagementType.BEAN), that would take away a big part of the point of using EJBs though. I do think this default behaviour is as it should be. If you don't want the Rollback in your log you can configure your logging level/etc - but I do think that EJB layered transactional rollbacks belongs in the log, it does definitely during development.

于 2012-08-06T18:23:41.073 回答