Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JDBC 라이브러리 구현하기 - 4단계] 후추(주찬민) 미션 제출합니다. #519

Merged
merged 9 commits into from
Oct 11, 2023

Conversation

Combi153
Copy link

@Combi153 Combi153 commented Oct 8, 2023

안녕하세요 주노 😃

어느덧 벌써 4단계네요.

시간이 정말 빠른 것 같아요.

이제는 아침, 저녁으로 기온이 쌀쌀하던데, 감기 조심하세요!

지난 PR에 커멘트 남겼어요.

4단계도 잘 부탁드립니다!

@Combi153 Combi153 requested a review from Choi-JJunho October 8, 2023 10:49
@Combi153 Combi153 self-assigned this Oct 8, 2023
Copy link
Member

@Choi-JJunho Choi-JJunho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 후추! 리뷰가 늦어져서 정말 죄송합니다 🙏
그치만 어제 클라이밍은 정말 즐거웠습니다 ⛰️

전체적으로 꼼꼼하게 잘 미션을 수행해주셨네요 👍
몇가지 의견을 들어보고싶은 내용이 있어 RC 남겨봅니다

Comment on lines 16 to 24
@Override
public User findById(long id) {
return userService.findById(id);
}

@Override
public void insert(User user) {
userService.insert(user);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findById와 insert에는 트랜잭션 적용이 필요없을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요구사항은 아니라고 생각해서, 적용하지 않았습니다. 하지만 적용해보면 좋을 것 같아요! 적용하겠습니다 👍


// 4단계 미션에서 사용할 것
// 4단계 미션에서 사용할 것 -> 넹
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹 >_0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

면 >_0

@@ -29,8 +30,8 @@ private <T> T execute(
PreparedStatementMaker pstmtMaker,
PreparedStatementExecuter<T> pstmtExecuter
) {
Connection conn = DataSourceUtils.getConnection(dataSource);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataSourceUtils를 사용하면서 외부의 connection을 JdbcTemplate가 알지 못해도 되는군요!
ThreadLocal을 사용함으로써 오버라이딩했던 메소드를 간단하게 고친 부분 좋습니다 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadLocal 로 트랜잭션을 관리하는 방법이 참신해서 재밌던 미션이에요! 👍

Comment on lines 25 to 27
conn.commit();
} catch (SQLException e) {
} catch (Exception e) {
rollback(conn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit 혹은 rollback 수행 시 connection을 close하지 않아도 될까요?
만약 롤백이 이뤄진 connection이 비정상적인 동작을 야기하지는 않을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection 을 close 해야 합니다.

제 코드에서는 finally 구문에서 DataSourceUtils 클래스의 releaseConnection 메서드를 호출해 connection을 close 하고 있습니다.

DataSourceUtils 클래스가 Connection을 열고 닫는 책임을 갖는 객체라고 생각했습니다. 따라서 DataSourceUtils 클래스의 releaseConnection 메서드 내부에서 connection을 닫는 선택을 했습니다.

TransactionManagerTest 에서 commit, rollback 각각에 대해 실제로 connection이 닫히는지 여부를 확인했습니다!

class TransactionManagerTest {
    // ...
    @Test
    void 예외가_발생하지_않으면_커밋된다() throws SQLException {
        // given
        TransactionManager transactionManager = new TransactionManager(dataSource);
        TransactionExecuter<String> transactionExecuter = conn -> "test";

        // when
        String result = transactionManager.execute(transactionExecuter);

        // then
        assertThat(result).isEqualTo("test");

        then(connection)
                .should(times(1))
                .commit();
        then(connection)
                .should(times(1))
                .close();
    }

    @Test
    void 예외가_발생하면_롤백된다() throws SQLException {
        // given
        TransactionManager transactionManager = new TransactionManager(dataSource);
        TransactionExecuter<String> transactionExecuter = conn -> {
            throw new SQLException();
        };

        // expect
        assertThatThrownBy(() -> transactionManager.execute(transactionExecuter))
                .isInstanceOf(DataAccessException.class);

        then(connection)
                .should(times(1))
                .rollback();
        then(connection)
                .should(times(1))
                .close();
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! DataSourceUtils 클래스에게 위임하셨군요 👍
명쾌한 답변 감사합니다~!

}

@Test
void Connection을_해제할_때_예외가_발생하면_Conection_을_닫지_못했다는_예외메시지를_전달한다() throws SQLException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼼꼼한 테스트 항상 감동받고갑니다 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따뜻한 리뷰 항상 감동받아요~ 😃

Copy link
Member

@Choi-JJunho Choi-JJunho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

후추~! Jdbc 미션 진행하느라 고생하셨습니다 👍
후추와 즐거운 토론을 할 수 있어서 저도 많이 배워가는 시간이였던 것 같아요😊

다음 미션도 화이팅입니다!!

@Choi-JJunho Choi-JJunho merged commit e83de98 into woowacourse:combi153 Oct 11, 2023
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants