-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 4단계] 조이(김성연) 미션 제출합니다. (#573)
* refactor: Transaction synchronization 적용 * refactor: 불필요 코드 제거 * refactor: 트랜잭션 서비스 추상화 * refactor: 컨벤션 통일 * fix: TransactionManager에서 RuntimeException 시 rollback 처리하도록 변경 * refactor: TransactionSynchronizationManager resources 초기화 로직 추가
- Loading branch information
Showing
17 changed files
with
174 additions
and
222 deletions.
There are no files selected for viewing
This file contains 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 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
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/techcourse/service/AppUserService.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public AppUserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, | ||
final String newPassword, | ||
final String createBy) { | ||
final User user = userDao.findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/techcourse/service/TxUserService.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionManager transactionManager; | ||
|
||
public TxUserService(final UserService userService, final TransactionManager transactionManager) { | ||
this.userService = userService; | ||
this.transactionManager = transactionManager; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionManager.execute( | ||
() -> userService.findById(id) | ||
); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionManager.execute( | ||
() -> { | ||
userService.insert(user); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, | ||
final String newPassword, | ||
final String createBy) { | ||
transactionManager.execute( | ||
() -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
} | ||
); | ||
} | ||
} |
This file contains 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,43 +1,12 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final TransactionManager transactionManager; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.transactionManager = new TransactionManager(DataSourceConfig.getInstance()); | ||
} | ||
void insert(final User user); | ||
|
||
public User findById(final long id) { | ||
return transactionManager.execute(connection -> userDao.findById(connection, id)); | ||
} | ||
|
||
public void insert(final User user) { | ||
transactionManager.execute(connection -> { | ||
userDao.insert(connection, user); | ||
return null; | ||
}); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.execute(connection -> { | ||
final var user = userDao.findById(connection, id); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
return null; | ||
} | ||
); | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
This file contains 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,8 +6,6 @@ | |
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.NoSuchElementException; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -30,56 +28,52 @@ class UserDaoTest { | |
|
||
private final DataSource dataSource = DataSourceConfig.getInstance(); | ||
|
||
private Connection connection; | ||
|
||
@BeforeEach | ||
void setup() throws SQLException { | ||
connection = dataSource.getConnection(); | ||
|
||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); | ||
jdbcTemplate.execute(connection, INIT_USER_TABLE_SQL); | ||
void setup() { | ||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); | ||
jdbcTemplate.execute(INIT_USER_TABLE_SQL); | ||
|
||
DatabasePopulatorUtils.execute(dataSource); | ||
userDao = new UserDao(dataSource); | ||
|
||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(connection, user); | ||
final User user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(connection); | ||
final var users = userDao.findAll(); | ||
|
||
assertThat(users).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void findById() { | ||
final var user = userDao.findById(connection, 1L); | ||
final var user = userDao.findById(1L); | ||
|
||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findById_fail() { | ||
assertThatThrownBy( | ||
() -> userDao.findById(connection, -1L) | ||
() -> userDao.findById(-1L) | ||
).isInstanceOf(NoSuchElementException.class) | ||
.hasMessage("id에 해당하는 user가 없습니다."); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var account = "gugu"; | ||
final var user = userDao.findByAccount(connection, account); | ||
final var user = userDao.findByAccount(account); | ||
|
||
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void findByAccount_fail() { | ||
assertThatThrownBy( | ||
() -> userDao.findByAccount(connection, "joy") | ||
() -> userDao.findByAccount("joy") | ||
).isInstanceOf(NoSuchElementException.class) | ||
.hasMessage("account에 해당하는 user가 없습니다."); | ||
} | ||
|
@@ -88,22 +82,22 @@ void findByAccount_fail() { | |
void insert() { | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(connection, user); | ||
userDao.insert(user); | ||
|
||
final var actual = userDao.findById(connection, 2L); | ||
final var actual = userDao.findById(2L); | ||
|
||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(connection, 1L); | ||
final var user = userDao.findById(1L); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(connection, user); | ||
userDao.update(user); | ||
|
||
final var actual = userDao.findById(connection, 1L); | ||
final var actual = userDao.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
This file contains 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
Oops, something went wrong.