-
Notifications
You must be signed in to change notification settings - Fork 379
[3단계 - Transaction 적용하기] 멍구(이유영) 미션 제출합니다. #1241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e619008
feat: 3단계 - Transaction 적용하기
YuyoungRhee 52b7b5e
fix: history update가 아닌 insert로 잘못된 로직 수정
YuyoungRhee a2f5d77
refactor: 트랜잭션 관리 로직을 jdbc/transaction 패키지로 분리
YuyoungRhee ac49ca4
refactor: UserService에서 모든 메서드가 connection을 외부에서 받도록 일관성 개선
YuyoungRhee bca3d5e
refactor: 쿼리빌더도 connection을 주입받도록 변경
YuyoungRhee 755aca2
test: 테스트에서의 connection 관리 개선
YuyoungRhee a4b56f3
refactor: 단일조회는 트랜잭션 적용 X, 단일 insert는 트랜잭션 적용
YuyoungRhee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,48 @@ | ||
| package com.techcourse.service; | ||
|
|
||
| import com.interface21.transaction.support.TransactionTemplate; | ||
| import com.techcourse.dao.UserDao; | ||
| import com.techcourse.dao.UserHistoryDao; | ||
| import com.techcourse.domain.User; | ||
| import com.techcourse.domain.UserHistory; | ||
| import java.sql.Connection; | ||
|
|
||
| public class UserService { | ||
|
|
||
| private final UserDao userDao; | ||
| private final UserHistoryDao userHistoryDao; | ||
| private final TransactionTemplate transactionTemplate; | ||
|
|
||
| public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
| public UserService(final UserDao userDao, | ||
| final UserHistoryDao userHistoryDao, | ||
| final TransactionTemplate transactionTemplate) { | ||
| this.userDao = userDao; | ||
| this.userHistoryDao = userHistoryDao; | ||
| this.transactionTemplate = transactionTemplate; | ||
| } | ||
|
|
||
| public User findById(final long id) { | ||
| public User getById(final long id) { | ||
| return userDao.findById(id) | ||
| .orElseThrow(() -> new IllegalArgumentException("해당 id의 user를 찾을 수 없습니다, id: " + id)); | ||
| } | ||
| }; | ||
|
|
||
| public void insert(final User user) { | ||
| userDao.insert(user); | ||
| transactionTemplate.execute(connection -> | ||
| userDao.insert(connection, user) | ||
| ); | ||
| } | ||
|
|
||
| public void changePassword(final long id, final String newPassword, final String createBy) { | ||
| final var user = findById(id); | ||
| user.changePassword(newPassword); | ||
| userDao.update(user); | ||
| userHistoryDao.log(new UserHistory(user, createBy)); | ||
| transactionTemplate.execute(connection -> { | ||
| final var user = getByIdInTransaction(connection, id); | ||
| user.changePassword(newPassword); | ||
| userDao.update(connection, user); | ||
| userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
| }); | ||
| } | ||
|
|
||
| private User getByIdInTransaction(final Connection connection, final long id) { | ||
| return userDao.findById(connection, id) | ||
| .orElseThrow(() -> new IllegalArgumentException("해당 id의 user를 찾을 수 없습니다, id: " + id)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,37 +6,50 @@ | |
| import com.interface21.jdbc.core.conversion.TypeConversionService; | ||
| import com.techcourse.domain.User; | ||
| import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import javax.sql.DataSource; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class UserDaoTest { | ||
|
|
||
| DataSource dataSource; | ||
| Connection connection; | ||
| UserDao userDao; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| DataSource dataSource = TestDataSourceConfig.create(); | ||
| void setup() throws SQLException { | ||
| dataSource = TestDataSourceConfig.create(); | ||
| connection = dataSource.getConnection(); | ||
| TypeConversionService typeConversionService = new TypeConversionService(); | ||
| JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource, typeConversionService); | ||
| DatabasePopulatorUtils.execute(dataSource); | ||
|
|
||
| userDao = new UserDao(jdbcTemplate); | ||
| final var user = new User("gugu", "password", "[email protected]"); | ||
| userDao.insert(user); | ||
| userDao.insert(connection, user); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws SQLException { | ||
| if (connection != null) { | ||
| connection.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void findAll() { | ||
| final var users = userDao.findAll(); | ||
| final var users = userDao.findAll(connection); | ||
|
|
||
| assertThat(users).isNotEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void findById() { | ||
| final var optionalUser = userDao.findById(1L); | ||
| final var optionalUser = userDao.findById(connection, 1L); | ||
|
|
||
| Assertions.assertThat(optionalUser) | ||
| .get() | ||
|
|
@@ -47,7 +60,7 @@ void findById() { | |
| @Test | ||
| void findByAccount() { | ||
| final var account = "gugu"; | ||
| final var optionalUser = userDao.findByAccount(account); | ||
| final var optionalUser = userDao.findByAccount(connection, account); | ||
|
|
||
| Assertions.assertThat(optionalUser) | ||
| .get() | ||
|
|
@@ -59,9 +72,9 @@ void findByAccount() { | |
| void insert() { | ||
| final var account = "insert-gugu"; | ||
| final var user = new User(account, "password", "[email protected]"); | ||
| userDao.insert(user); | ||
| userDao.insert(connection, user); | ||
|
|
||
| final var optionalActual = userDao.findById(2L); | ||
| final var optionalActual = userDao.findById(connection, 2L); | ||
|
|
||
| Assertions.assertThat(optionalActual) | ||
| .get() | ||
|
|
@@ -73,14 +86,14 @@ void insert() { | |
| void update() { | ||
| // given | ||
| final var newPassword = "password99"; | ||
| final var user = userDao.findById(1L).get(); | ||
| final var user = userDao.findById(connection, 1L).get(); | ||
| user.changePassword(newPassword); | ||
|
|
||
| // when | ||
| userDao.update(user); | ||
| userDao.update(connection, user); | ||
|
|
||
| // then | ||
| final var optionalActual = userDao.findById(1L); | ||
| final var optionalActual = userDao.findById(connection, 1L); | ||
|
|
||
| Assertions.assertThat(optionalActual) | ||
| .get() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,41 +6,57 @@ | |
| import com.interface21.dao.DataAccessException; | ||
| import com.interface21.jdbc.core.JdbcTemplate; | ||
| import com.interface21.jdbc.core.conversion.TypeConversionService; | ||
| import com.interface21.transaction.support.TransactionTemplate; | ||
| import com.techcourse.config.DataSourceConfig; | ||
| import com.techcourse.dao.UserDao; | ||
| import com.techcourse.dao.UserHistoryDao; | ||
| import com.techcourse.domain.User; | ||
| import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import javax.sql.DataSource; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @Disabled | ||
| class UserServiceTest { | ||
|
|
||
| private JdbcTemplate jdbcTemplate; | ||
| private UserDao userDao; | ||
| JdbcTemplate jdbcTemplate; | ||
| UserDao userDao; | ||
| TransactionTemplate transactionTemplate; | ||
| DataSource dataSource; | ||
| Connection connection; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance(), new TypeConversionService()); | ||
| void setUp() throws SQLException { | ||
| dataSource = DataSourceConfig.getInstance(); | ||
| connection = dataSource.getConnection(); | ||
| transactionTemplate = new TransactionTemplate(dataSource); | ||
| this.jdbcTemplate = new JdbcTemplate(dataSource, new TypeConversionService()); | ||
| this.userDao = new UserDao(jdbcTemplate); | ||
|
|
||
| DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
| DatabasePopulatorUtils.execute(dataSource); | ||
| final var user = new User("gugu", "password", "[email protected]"); | ||
| userDao.insert(user); | ||
| userDao.insert(connection, user); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws SQLException { | ||
| if (connection != null) { | ||
| connection.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testChangePassword() { | ||
| final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
| final var userService = new UserService(userDao, userHistoryDao); | ||
| final var userService = new UserService(userDao, userHistoryDao, transactionTemplate); | ||
|
|
||
| final var newPassword = "qqqqq"; | ||
| final var createBy = "gugu"; | ||
| userService.changePassword(1L, newPassword, createBy); | ||
|
|
||
| final var actual = userService.findById(1L); | ||
| final var actual = userService.getById(1L); | ||
|
|
||
| assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
| } | ||
|
|
@@ -49,15 +65,15 @@ void testChangePassword() { | |
| void testTransactionRollback() { | ||
| // 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
| final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
| final var userService = new UserService(userDao, userHistoryDao); | ||
| final var userService = new UserService(userDao, userHistoryDao, transactionTemplate); | ||
|
|
||
| final var newPassword = "newPassword"; | ||
| final var createBy = "gugu"; | ||
| // 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
| assertThrows(DataAccessException.class, | ||
| () -> userService.changePassword(1L, newPassword, createBy)); | ||
|
|
||
| final var actual = userService.findById(1L); | ||
| final var actual = userService.getById(1L); | ||
|
|
||
| assertThat(actual.getPassword()).isNotEqualTo(newPassword); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이번 미션은 JdbcTemplate에 집중하는 거니 어쩔 수 없지만..
필드목록을 하나하나 전부 매핑해줘야 하는게 아쉽긴 하네요 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞습니다.. 그러나 어떤 필드가 DB에서 어떤 컬럼명으로 되어있는지는 개발자가 직접 지정해줘야하기 때문에 현재는 이게 최선인 것 같아요.
JPA처럼 @column("user_id") 처럼 엔티티 필드에 정의해두고, 리플렉션으로 읽어서 자동으로 insert문을 만들어주는 아이디어가 있겠지만 이번 미션에서는 구현하지 않을게요! 모코는 이번 미션에서 이런 문제를 해결하기 위해 어떤 시도들을 하셨나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 말씀해주신 것처럼 어노테이션 기반 리플렉션을 통한 필드 자동 매핑을 구현했습니다.
미션 목표는 JdbcTemplate 구현이었지만 저는 Jpa 구현에 초점을 두고 진행했던 것 같아요.
그게 더 재밌어서요!! 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jpa 구현 당시 PR 첨부드립니다 😄
#1118