Skip to content

Instantly share code, notes, and snippets.

@gksxodnd007
Created August 13, 2018 06:40
Show Gist options
  • Select an option

  • Save gksxodnd007/1cbec0b02572fcdcd12d8acd43b0de5b to your computer and use it in GitHub Desktop.

Select an option

Save gksxodnd007/1cbec0b02572fcdcd12d8acd43b0de5b to your computer and use it in GitHub Desktop.
JPA 트랜잭션 롤백

Spring Data JPA에서 트랜잭션 롤백

  • 보통 spring data를 이용하면 영속성 컨텍스트의 생명주기는 트랜잭션의 유지시간과 동일함.
    • exception이 발생하여 롤백 시 트랜잭션이 닫히면서 자연스럽게 영속성 컨텍스트도 닫힌다. 문제 발생할 여지 없음
  • OSIV처럼 영속성 컨텍스트의 생명주기가 트랜잭션보다 길 때 발생 할 수 있는 문제점.
    • 데이터베이스에는 데이터가 반영되지 않았지만 영속성 컨텍스트에는 데이터가 잔류 되어있다.
    • spring frame work에서는 다음과 같은 방법으로 문제를 해결한다.
      • 트랜잭션 롤백 시 entityManager.clear()를 호출하여 영속성 컨텍스트를 초기화한다.

관련 코드

protected void doRollback(DefaultTransactionStatus status) {
        JpaTransactionManager.JpaTransactionObject txObject = (JpaTransactionManager.JpaTransactionObject)status.getTransaction();
        if (status.isDebug()) {
            this.logger.debug("Rolling back JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]");
        }

        try {
            EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
            if (tx.isActive()) {
                tx.rollback();
            }
        } catch (PersistenceException var7) {
            throw new TransactionSystemException("Could not roll back JPA transaction", var7);
        } finally {
            if (!txObject.isNewEntityManagerHolder()) {
                txObject.getEntityManagerHolder().getEntityManager().clear();
            }

        }

    }
  • 위의 코드는 JpaTransactionManager의 메소드이다. try-catch-finally 구문중 finally 부분을 확인해보면 entityManager.clear()를 호출하는 것을 볼 수 있다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment