2016-06-15 1 views
2

봄삭제 작업 중에 발생하는 예외를 무시하고 싶습니다.데이터베이스 ID가 더 이상 존재하지 않으면 삭제시 예외를 무시하는 방법?

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

문제 : (- 그것은 동시에 삭제되었을 수 있습니다 의미 ID가 데이터베이스에 더 이상 존재하지 않는 삭제 될 경우 예) 나는이 프로그램을 실행할 때, 나는 여전히 다음과 같은 예외를 받고 있어요. 내가 어떻게 그것을 무시할 수 있을까?

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] 
+0

: 제 생각에는

이 솔루션은 findOne 방법 및 엔티티가 전화를 delete 방법이있는 경우 다음 (ID에 의해 기관이 아닌 방법을 삭제 호출)를 호출하는 것입니다 . 나열한 코드는 게시 한 예외를 throw하지 않습니다. – EJP

+0

글쎄, 로그는'dao.delete()'가있는 곳의 코드 라인을 가리키고 있습니다. – membersound

답변

-1
@Tranactional(rollbackFor={TableNotFoundException1.class, RecordNotFoundException.class} 
public void remove(Long id) { 
    try { 
     if (id != null) dao.delete(id); //CrudRepository 
    } catch (Exception1 e) { 
     throw new TableNotFoundException1(); 
    }catch (Exception2 e) { 
     throw new RecordNotFoundException(); 
    }catch (Exception e) { 
     throw new RecordNotFoundException(); 
    } 
} 

당신은 당신이 Exception1, Exception2을 잡을하거나 왜 rollbackFor를 선언 할 필요가 rollbackFor={Exception.class}

Exception을 사용하는 방법을 찾을 수있다? 기본적으로 Spring은 체크되지 않은 예외 만 롤백합니다.

기타 옵션 : 당신이 말에 커밋 그렇게 할 때, 같은 rollbackonly 당신이 예외를 잡을 경우에도 트랜잭션 관리자는 트랜잭션을 표시하는 것 또한 작동 후,하지만 @Tranactional

//@Tranactional(rollbackFor={TableNotFoundException1.class, RecordNotFoundException.class} 
public void remove(Long id) { 
    try { 
     if (id != null) dao.delete(id); //CrudRepository 
    }catch (Exception e) { 
     TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
    } 
} 
+0

@andolsizied 봄을 확인하세요. docs + https://stackoverflow.com/questions/7125837/why-does-transaction-roll-back-on-runtimeexception-but-not-sqlexception/7125918#7125918 – sura2k

0

를 사용하지 않고 메소드를 사용하면 TransactionSystemException 예외가 발생합니다.

첫 번째 반사는 방법 @Transactional(noRollbackFor=EmptyResultDataAccessException.class)를 표시하는 것입니다하지만 당신은 delete 방법은 transactional로 표시되는 것을 볼 수 줘야 SimpleJpaRepository 클래스를 살펴 경우 때문에이 문제가 해결되지 않기 때문에 예외가 발생하는 경우,이 @Transactional는 마크 트랜잭션을 롤백으로 만 사용합니다. 삭제 작업을하는 동안이 예외가 발생되지

@Tranactional 
public void remove(Long id) { 
    if (id != null) { 
    YouEntity entity= dao.findOne(id); 
    if (nonNull(entity)) { 
     dao.delete(entity); 
    } 
    } 
} 
관련 문제