javax.persistence.RollbackException: Transaction marked as rollbackOnly

廉价感情. 提交于 2021-01-28 05:47:09

问题


I'm making a game 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

@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

@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?


回答1:


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.:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)


来源:https://stackoverflow.com/questions/32034292/javax-persistence-rollbackexception-transaction-marked-as-rollbackonly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!