相关疑难解决方法(0)

如何防止JPA回滚交易?

调用的方法:
1.Struts Action
2.服务类方法(由@Transactional注释)
3.Xfire webservice调用

包括struts(DelegatingActionProxy)和事务在内的所有东西都是用Spring配置的.

使用JPA/Hibernate完成持久化.

有时web服务会抛出一个未经检查的异常.我捕获此异常并抛出一个已检查的异常.我不希望事务回滚,因为Web服务异常会更改当前状态.我已经注释了这样的方法:

@Transactional(noRollbackFor={XFireRuntimeException.class, Exception.class})
public ActionForward callWS(Order order, ....) throws Exception
  (...)
  OrderResult orderResult = null;

  try {
    orderResult = webService.order(product, user)
  } catch (XFireRuntimeException xfireRuntimeException) {
    order.setFailed(true);
    throw new WebServiceOrderFailed(order);
  } finally {
    persist(order);
  }
}
Run Code Online (Sandbox Code Playgroud)

我仍然得到这个例外:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
Run Code Online (Sandbox Code Playgroud)

当我尝试使用junit重现此事务时,事务未标记为回滚,并且仍可以提交事务.

如何让Spring不回滚事务?

java spring struts hibernate jpa

9
推荐指数
1
解决办法
5万
查看次数

在JPA + Spring中发生异常后回滚事务

我在下面使用HIbernate和Spring和JPA。当引发PersistenceException时,我想捕获它并返回错误消息,以便它不会传播到调用者。

@Transactional
public String save(Object bean) {
    String error = null;

    try {
        EntityManager entityManager = getEntityManager();

        for (int i = 0, n = entities.size(); i < n; i ++) {
            entityManager.merge(entities.get(i));
        }
    }
    catch (PersistenceException e) {
        error = e.getMessage();
    }

    return error;
}
Run Code Online (Sandbox Code Playgroud)

但是我得到一个异常,说javax.persistence.RollbackException: Transaction marked as rollbackOnly.我需要在异常之后回滚事务,但是当我捕获到异常并且不想重新抛出该异常时,如何回滚它呢?

java spring hibernate jpa

4
推荐指数
3
解决办法
2万
查看次数

标签 统计

hibernate ×2

java ×2

jpa ×2

spring ×2

struts ×1