前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javax.persistence.RollbackException: Transaction marked as rollbackOnly Askjavax.persistence.Rollba

javax.persistence.RollbackException: Transaction marked as rollbackOnly Askjavax.persistence.Rollba

作者头像
一个会写诗的程序员
发布2018-08-20 10:33:25
1K0
发布2018-08-20 10:33:25
举报

javax.persistence.RollbackException: Transaction marked as rollbackOnly

with Spring MVC. I have a method for get the game and update but when it goes to update it gives an error, this is the code:

HomeController.class

代码语言:javascript
复制
@Transactional
@RequestMapping(value = "/partida/{idPartida}", method = RequestMethod.GET)
public String getPartida(@PathVariable("idPartida") long idPartida,
        Model model) throws IOException {
    Partida p = ServicioAplicacionPartida.getPartida(entityManager,
            idPartida);

    if (p.getJson() == null) {
        p.inicializarPartida(entityManager);
        ServicioAplicacionPartida.update(entityManager, p);
    }

GameDAO.class

代码语言:javascript
复制
@Transactional
public static Partida update(EntityManager entityManager, Partida p) {
    try {               
        Query q = entityManager.createNativeQuery("update Partida p SET p.json=:json where p.id=:id");
        q.setParameter("json", p.getJson());
        q.setParameter("id", p.getId());
        q.executeUpdate();
        return entityManager.find(Partida.class, p.getId());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

The error occurs when the line "q.executeUdate()" is executed, here it is: javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute statement And this is the server error: Estado HTTP 500 - Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly What can i do to fix it?

You have annotated controller and DAO methods as @Transactional, it is not correct as @Transactional can be inherited to the inner methods. Usually transaction should start at service layer.

Try adding these parameters to @Transactional annotation and remove it from either Controller or DAO.:

代码语言:javascript
复制
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)

and try:

代码语言:javascript
复制
propagation = Propagation.REQUIRES_NEW
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • javax.persistence.RollbackException: Transaction marked as rollbackOnly
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档