Fix could not commit JPA transaction rollbackexception:transaction marked as Rollbackonly

Source: Internet
Author: User

The project test had a problem, the method ended normally, but reported

Could not commit JPA transaction, nested exception is javax.persistence.RollbackException:Transaction marked as ROLLB Ackonly org.springframework.transaction.TransactionSystemException:Could not commit JPA transaction; Nested exception is javax.persistence.RollbackException:Transaction marked as rollbackonly at Org.springframework.orm. Jpa. Jpatransactionmanager.docommit (Jpatransactionmanager.java:521) at Org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit ( Abstractplatformtransactionmanager.java:754) at Org.springframework.transaction.support.AbstractPlatformTransactionManager.commit ( Abstractplatformtransactionmanager.java:723) at Org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning ( Transactionaspectsupport.java:393) at Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (Transactioninterceptor.java: 120) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 172) at Org.springframework.aop.framework.cglib2aopproxy$dynamicadvisedinterceptor.intercept ( Cglib2aopproxy.java:622)

Error, ask what can not be submitted?

After the search, I found this passage.

 I finally   understood the Problem:methoda () {MethodB ()} @Transactional (norollbackfor  = Exception. class   try   {METHODC ()}  catch   (...)    {...} Log ( OK );} @TransactionalmethodC () { throw  new   ... ();} What happens was that even though the MethodB have the right annotation, the METHODC does not. When the exception are thrown, the second @Transactional marks the first transaction as Rollback only anyway.  

Originally, in a transactional if there is another transaction exception, even if you catch the exception, then transaction will also be defined as rollbackonly, which is the principle of transaction management, but my system where the exception?

Originally, the Spring JPA Jparepository implementation method with the ID deleted by the source code is this

@Transactional      Public void Delete (ID ID) {        assert.notnull (ID, id_must_not_be_null);         = FindOne (ID);         if NULL ) {            thrownew emptyresultdataaccessexception (String.Format ("No%s entity with ID%s exists! " ,                     1);        }        Delete (entity);    }

He did so by looking for the object of this ID to see if there is a non-checked exception that is thrown directly if it does not exist, and the delete operation is no longer performed. This is not the same as what I used to think, generally I am accustomed to delete no records will not error, the implementation of SQL is also the case. And I just caught this anomaly outside, so the problem happened.

Resolve could not commit JPA transaction rollbackexception:transaction marked as Rollbackonly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.