웹 프로그래밍/[ Spring Data JPA ]

[ Spring Data JPA ] 02. Transaction Commit은 어디에서 일어날까?

kim.svadoz 2021. 10. 18. 17:50
728x90
반응형

트랜잭션 커밋은 어디에서 일어날까?

레파지토리를 만들 때 Spring-Data-JpaJpaRepository 인터페이스를 상속하였는데, 스프링 데이터에서 기본 구현체를 제공해주기 때문이다.

 

Spring-Data-Jpa에서 제공하는 JpaRepository의 기본 구현체는 SimpleJpaRepository이다.

(CrudRepository<>는 단순히 인터페이스이다.)

SimpleJpaRepositorysave()메소드에는 스프링 @Transactional이 붙어있으므로 해당 클래스에 있는 수많은 메소드에 트랜잭션이 걸리게 되고, 메소드 성공적으로 return하게 되면 commit도 이루어지게 되는것이다.

@Transactional
public <S extends T> S save(S entity) {
    Assert.notNull(entity, "Entity must not be null.");
    if (this.entityInformation.isNew(entity)) {
        this.em.persist(entity);
        return entity;
    } else {
        return this.em.merge(entity);
    }
}

따라서, save() 메소드와 같이 CRUD method()를 수행하는 시점이 트랜잭션의 시작 과 종료 커밋 시점임을 알 수 있다.

 

728x90
반응형