Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore exceptions on delete if database id not exists anymore?

I'd like to ignore any exceptions that occur during a delete action in spring CrudRepository.

@Tranactional
public void remove(Long id) {
    try {
        if (id != null) dao.delete(id); //CrudRepository
    } catch (Exception e) {
        //ignore any exceptions, it's not critical delete
    }
}

Problem: when I run this, I'm still getting the following exception (eg if the id to be deleted does not exist anymore in database - means it may have been deleted concurrently). How could I ignore it?

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:526) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
like image 736
membersound Avatar asked Jun 15 '16 11:06

membersound


1 Answers

Even if you catch the exception, the transaction manager will mark the transaction as rollbackonly, so when you commit at the end of the method you will have an TransactionSystemException exception.

The first reflex is to mark the method @Transactional(noRollbackFor=EmptyResultDataAccessException.class) but this does not solve the problem because if you take a look in SimpleJpaRepository class wil see that delete method is marked as transactional, so when the exception is thrown, this @Transactional marks your transaction as rollback only.

In my opinion, the solution is to call findOne method and then if entity exists call delete method (call delete method by entity and not by ID) :

@Tranactional
public void remove(Long id) {
    if (id != null) {
     YouEntity entity= dao.findOne(id);
     if (nonNull(entity)) {
        dao.delete(entity);
     }
    }
 }
like image 183
andolsi zied Avatar answered Oct 23 '22 23:10

andolsi zied