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
@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?
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)
and try:
propagation = Propagation.REQUIRES_NEW
网友评论