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단계] 로지(윤가영) 미션 제출합니다. #561

Merged
merged 7 commits into from
Oct 12, 2023

Conversation

kyY00n
Copy link
Member

@kyY00n kyY00n commented Oct 9, 2023

안녕하세요 가비 ^^~
4단계를 한번 구현해보았습니다.
저는 구현하다 막판에 고민되는 점이 하나 있었는데요,

TxUserService에서의 트랜잭션 경계 부분 말이죠
예시코드에서

public void changePassword(final long id, final String newPassword, final String createBy) {
    Connection connection = DataSourceUtils.getConnection(dataSource);
    connection.setAutoCommit(false);

    try {
        // todo
        connection.commit();
    } catch (...) {
        connection.rollback();
        ...
    } finally {
        DataSourceUtils.releaseConnection(connection, dataSource);
        TransactionSynchronizationManager.unbindResource(dataSource); // !
    }
}

다음과 같이 finally 부분에서 TransactionSynchronizationManaber 의 unbindResource(dataSource); 를 해주는 게 살짝 이해가 되지 않았어요.

왜냐하면 TransactionSynchronizationManager의 bindResource는 TxUserService가 아닌 DataSourceUtils에서 호출해주고 있었기 때문이에요.
그래서 unbind 해주는 것도 제가 생각했을 때는 TransactionSynchronizationManager에서 해주는게 맞다고 생각했어요.
저는 제가 말한 방식으로 구현했는데 문제는 찾지 못했습니다. 가비는 어떻게 생각하나요?

@kyY00n kyY00n requested a review from iamjooon2 October 9, 2023 05:47
Copy link

@iamjooon2 iamjooon2 left a comment

Choose a reason for hiding this comment

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

안녕하세요 로지
평안하시죠?

바로 옆에 있는데 코멘트 좀 적는 기분 좀 신기하네요
로지 코멘트 보고 제 코드 고쳤습니다ㅎㅎ;

흠잡을 곳이 없네요🥲

다음 리뷰요청 주시면 머지하겠습니다
이번 미션도 고생많으셨습니다

Comment on lines 18 to 20
if (dataSourceConnectionMap == null) {
return null;
}

Choose a reason for hiding this comment

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

방어로직 좋네요👍

Comment on lines +93 to 97
final Connection conn = getConnection();
try (final PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
log.debug("query : {}", sql);

setParameters(preparedStatement, parameters);

Choose a reason for hiding this comment

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

TxUserService가 아닌 jdbctemplate을 바로 사용한다면 connection이 닫히지 않을 것 같습니다...!

Comment on lines 29 to 45
final var dataSource = DataSourceConfig.getInstance();
final var connection = DataSourceUtils.getConnection(dataSource);
try {
connection.setAutoCommit(false);
userService.changePassword(id, newPassword, createBy);

connection.commit();
} catch (final Exception e) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new DataAccessException(ex);
}
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
}

Choose a reason for hiding this comment

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

조금 더 챌린지 하실게 필요하시다면
트랜잭션 부분만 transactionTemplate으로 추상화하는 것은 어떨까요?

Copy link

@iamjooon2 iamjooon2 left a comment

Choose a reason for hiding this comment

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

고생많으셨습니다 ☀️

Comment on lines +23 to +26
execute(DataSourceConfig.getInstance(), () -> {
userService.insert(user);
return null;
});

Choose a reason for hiding this comment

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

👍👍 깔끔하네요

@iamjooon2 iamjooon2 merged commit a39336b into woowacourse:kyy00n Oct 12, 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