JavaBookStudy / JavaBook

책읽기 스터디
https://javabookstudy.github.io/
Apache License 2.0
19 stars 2 forks source link

[토비의 스프링] 6.8.2_트랜잭션 매니저와 @Transactional #121

Closed kjsu0209 closed 3 years ago

kjsu0209 commented 3 years ago

트랜잭션 매니저로 트랜잭션 범위를 설정하기 위해 DefaultTransactionDefinitionTransactionStatus를 설정하고, 롤백, 커밋은 transactionManager에서 메서드를 호출하는 방식으로 사용됩니다.

그럼 @Transactional은 원래 트랜잭션 매니저로 설정하기 위해 작성해야 했던 코드를 AOP로 간편하게 쓸 수 있게 만든 건가요? 둘의 내부 작동 방식이 같은지 궁금합니다 👀

daebalprime commented 3 years ago

https://www.marcobehler.com/guides/spring-transaction-management-transactional-in-depth 에 따르면 아래 두 코드는 동일하다고 합니다.

`public class UserService {

public Long registerUser(User user) {
    Connection connection = dataSource.getConnection(); // (1)
    try (connection) {
        connection.setAutoCommit(false); // (1)

        // execute some SQL that e.g.
        // inserts the user into the db and retrieves the autogenerated id
        // userDao.save(user); <(2)

        connection.commit(); // (1)
    } catch (SQLException e) {
        connection.rollback(); // (1)
    }
}

}`

`public class UserService {

public Long registerUser(User user) {
    Connection connection = dataSource.getConnection(); // (1)
    try (connection) {
        connection.setAutoCommit(false); // (1)

        // execute some SQL that e.g.
        // inserts the user into the db and retrieves the autogenerated id
        // userDao.save(user); <(2)

        connection.commit(); // (1)
    } catch (SQLException e) {
        connection.rollback(); // (1)
    }
}

}`

더 자세한 내용은 잘 모르겠습니다... 죄송합니다 ㅎㅎ;