-
Notifications
You must be signed in to change notification settings - Fork 300
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단계] 쥬니(전정준) 미션 제출합니다. #516
Changes from 5 commits
f17eaa8
046a624
db55757
9520ed7
efb556e
fd3b7d7
cd7f0c5
97eefed
01d5b39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(UserDao userDao, UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
User user = findById(id); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final TransactionManager transactionManager; | ||
private final UserService appUserService; | ||
|
||
public TxUserService(TransactionManager transactionManager, UserService appUserService) { | ||
this.transactionManager = transactionManager; | ||
this.appUserService = appUserService; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return appUserService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
transactionManager.start(); | ||
try { | ||
appUserService.insert(user); | ||
|
||
transactionManager.commit(); | ||
} catch (DataAccessException e) { | ||
transactionManager.rollback(); | ||
throw e; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
transactionManager.start(); | ||
try { | ||
appUserService.changePassword(id, newPassword, createBy); | ||
|
||
transactionManager.commit(); | ||
} catch (DataAccessException e) { | ||
transactionManager.rollback(); | ||
throw e; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 지난 번 리뷰에서도 말씀 드렸었는데, 지금은 Service도 몇개 안되고 그안에 메서드들도 몇개 안되지만 이게 많아지면 라이브러리 사용자가 너무 구현하기 힘들 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try-catch 중복은 제거 완료했습니다. 어노테이션 방법으로 구현해보고 싶어서, 한 시간정도 시도해보았는데요. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,13 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.transaction.Transaction; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final TransactionManager transactionManager; | ||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
User findById(long id); | ||
|
||
public UserService(TransactionManager transactionManager, UserDao userDao, UserHistoryDao userHistoryDao) { | ||
this.transactionManager = transactionManager; | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
void insert(User user); | ||
|
||
public User findById(long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(User user) { | ||
Transaction transaction = transactionManager.getTransaction(); | ||
transaction.start(); | ||
|
||
try { | ||
userDao.insert(transaction.getConnection(), user); | ||
transaction.commit(); | ||
} catch (DataAccessException e) { | ||
transaction.rollback(); | ||
throw e; | ||
} | ||
} | ||
|
||
public void changePassword(long id, String newPassword, String createBy) { | ||
Transaction transaction = transactionManager.getTransaction(); | ||
transaction.start(); | ||
try { | ||
Connection connection = transaction.getConnection(); | ||
User user = findById(id); | ||
|
||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
transaction.commit(); | ||
} catch (DataAccessException e) { | ||
transaction.rollback(); | ||
throw e; | ||
} | ||
} | ||
void changePassword(long id, String newPassword, String createBy); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,18 +19,16 @@ class UserDaoTest { | |
|
||
private final JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
private UserDao userDao; | ||
private DataSource dataSource; | ||
|
||
@BeforeEach | ||
void setup() throws SQLException { | ||
dataSource = DataSourceConfig.getInstance(); | ||
DatabasePopulatorUtils.execute(dataSource); | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
|
||
jdbcTemplate.update(dataSource.getConnection(), "TRUNCATE TABLE users RESTART IDENTITY"); | ||
jdbcTemplate.update("TRUNCATE TABLE users RESTART IDENTITY"); | ||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
|
||
User user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(dataSource.getConnection(), user); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
|
@@ -42,7 +40,7 @@ void findAll() { | |
|
||
@Test | ||
void findById() throws SQLException { | ||
userDao.insert(dataSource.getConnection(), new User("gugu", "password", "[email protected]")); | ||
userDao.insert(new User("gugu", "password", "[email protected]")); | ||
User user = userDao.findById(1L); | ||
|
||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
|
@@ -60,7 +58,7 @@ void findByAccount() { | |
void insert() throws SQLException { | ||
String account = "insert-gugu"; | ||
User user = new User(account, "password", "[email protected]"); | ||
userDao.insert(dataSource.getConnection(), user); | ||
userDao.insert(user); | ||
|
||
User actual = userDao.findById(2L); | ||
|
||
|
@@ -73,7 +71,7 @@ void update() throws SQLException { | |
User user = userDao.findById(1L); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(dataSource.getConnection(), user); | ||
userDao.update(user); | ||
|
||
User actual = userDao.findById(1L); | ||
|
||
|
@@ -88,7 +86,7 @@ void findByAccount_FailByMultipleResults() throws SQLException { | |
assertThat(findUser).isNotNull(); | ||
|
||
User duplicateUser = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(dataSource.getConnection(), duplicateUser); | ||
userDao.insert(duplicateUser); | ||
|
||
//when then | ||
assertThatThrownBy(() -> userDao.findByAccount("gugu")) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,36 +8,35 @@ | |
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
class UserServiceTest { | ||
class AppUserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
private TransactionManager transactionManager; | ||
|
||
@BeforeEach | ||
void setUp() throws SQLException { | ||
void setUp() { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
this.transactionManager = new TransactionManager(dataSource); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
DatabasePopulatorUtils.execute(dataSource); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(dataSource.getConnection(), user); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void testChangePassword() { | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(transactionManager, userDao, userHistoryDao); | ||
final var userService = new AppUserService(userDao, userHistoryDao); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
|
@@ -52,7 +51,10 @@ void testChangePassword() { | |
void testTransactionRollback() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(transactionManager, userDao, userHistoryDao); | ||
// 애플리케이션 서비스 | ||
final var appUserService = new AppUserService(userDao, userHistoryDao); | ||
// 트랜잭션 서비스 추상화 | ||
final var userService = new TxUserService(transactionManager, appUserService); | ||
|
||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
|
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.
P?:
이 부분은 트랜잭션 처리를 안하신 이유가 따로 있으신가요??
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.
이 부분에 대해서 질문하실거라고 생각했어요 ㅎㅎ
조회용 트랜잭션(readOnly)을 만들기 위해서, 부가적인 기능을 구현해야한다는 점에서 귀찮음을 느꼈어요..
하지만, 빠르게 구현했습니당 ~!