javax.persistence.RollbackException: Transaction marked as rollbackOnly Ask

简介: javax.persistence.RollbackException: Transaction marked as rollbackOnlywith Spring MVC.

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

相关文章
|
6月前
|
Go
【已解决】SendTransactionVM Exception while processing transaction: Transaction‘s maxFeePerGas (200000000
【已解决】SendTransactionVM Exception while processing transaction: Transaction‘s maxFeePerGas (200000000
43 0
ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'.
ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'.
|
SQL 关系型数据库 MySQL
org.springframework.jdbc.BadSqlGrammarException: Error updating database
org.springframework.jdbc.BadSqlGrammarException: Error updating database
251 0
|
Web App开发 Java 数据库连接
javax.validation.ValidationException: Unable to create a Configuration
错误信息: [org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean]-Failed to set up a Bean Validation provider javax.
3652 0
|
开发框架 Java 数据库连接
Persistence,EntityManagerFactory和EntityManager
Persistence,EntityManagerFactory和EntityManager
610 0
【shiro】报错: If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.
spring整合shiro,项目报如下错误: 1 ==============异常开始============= 2 java.lang.IllegalStateException: The mapped controller method class 'com.
2884 0
问题:org.hibernate.LazyInitializationException: failed to lazily initialize
今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的session中去调用lazy=“true“的属性, 网上资料蛮多的,解决方法有两个 1,把lazy=”false“ 2,在web.
903 0