-
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단계] 준팍(박준현) 미션 제출합니다. #607
Changes from all commits
6d5952e
b9759ba
131f9e0
c58d27d
697db42
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(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
final var 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,31 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final TransactionManager transactionManager; | ||
private final UserService userService; | ||
|
||
public TxUserService(TransactionManager transactionManager, UserService userService) { | ||
this.transactionManager = transactionManager; | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return transactionManager.query(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
transactionManager.execute(() -> userService.insert(user)); | ||
} | ||
|
||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
transactionManager.execute(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +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 java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
|
||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
Connection connection = null; | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
connection.setAutoCommit(false); | ||
|
||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (RuntimeException | SQLException e) { | ||
rollback(connection); | ||
throw new DataAccessException(e); | ||
} finally { | ||
close(connection); | ||
} | ||
} | ||
|
||
private void rollback(Connection connection) { | ||
if (connection == null) { | ||
return; | ||
} | ||
try { | ||
connection.rollback(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
private void close(final Connection connection) { | ||
if (connection == null) { | ||
return; | ||
} | ||
try { | ||
connection.close(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
void insert(final User user); | ||
|
||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.support.TransactionManager; | ||
|
||
class UserServiceTest { | ||
|
||
|
@@ -31,7 +32,7 @@ void setUp() { | |
@Test | ||
void testChangePassword() { | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, userHistoryDao); | ||
final var userService = new AppUserService(userDao, userHistoryDao); | ||
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. 여기선 AppUserService를 테스트하셨군요 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. 테스트 내에서 Transaction 관련한 로직이 없기 때문에, |
||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
|
@@ -46,7 +47,12 @@ void testChangePassword() { | |
void testTransactionRollback() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, userHistoryDao); | ||
// 애플리케이션 서비스 | ||
final var appUserService = new AppUserService(userDao, userHistoryDao); | ||
TransactionManager transactionManager = new TransactionManager(DataSourceConfig.getInstance()); | ||
|
||
// 트랜잭션 서비스 추상화 | ||
final var userService = new TxUserService(transactionManager,appUserService); | ||
|
||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.springframework.transaction; | ||
|
||
public class TransactionException extends RuntimeException { | ||
|
||
public TransactionException(Exception e) { | ||
super(e); | ||
} | ||
|
||
} |
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.
추후 발생할 수 있는 트랜잭션 분리를 생각해보시는 것도 재밌을 것 같네요😀
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.
이거 무엇에 관한 질문인지 이해를 못했는데
다시 설명해주실 수 있나요?
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.
아 Transactional의 required_new를 구현하는 것에 대한 얘기였어요