-
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단계] 민트(유재민) 미션 제출합니다. #458
Changes from 18 commits
23f8a48
fd3b51e
161d0af
9ce3a83
696a787
93261f4
94d40f9
055eb1b
34ffba0
3c484ec
e205b8f
df7e776
b33cfd1
36fbd06
7e30cef
3438d8d
5ad575e
13f7d03
d0e93ca
c11f4f4
c2efb84
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,33 @@ | ||
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; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("유저가 존재하지 않습니다.")); | ||
} | ||
|
||
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); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
public TxUserService(final UserService userService, final TransactionTemplate transactionTemplate) { | ||
this.userService = userService; | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionTemplate.executeWithTransaction(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionTemplate.executeWithoutResult(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.executeWithoutResult(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,10 @@ | ||
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.support.TransactionTemplate; | ||
|
||
import javax.sql.DataSource; | ||
public interface UserService { | ||
|
||
public class UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
final DataSource dataSource = DataSourceConfig.getInstance(); | ||
this.transactionTemplate = new TransactionTemplate(dataSource); | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("유저가 존재하지 않습니다.")); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.executeWithTransaction(() -> { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
}); | ||
} | ||
User findById(final long id); | ||
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 |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
@@ -18,11 +21,14 @@ class UserServiceTest { | |
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
private TransactionTemplate transactionTemplate; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
this.transactionTemplate = new TransactionTemplate(dataSource); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
|
@@ -32,7 +38,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); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
|
@@ -47,7 +53,10 @@ 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); | ||
// 트랜잭션 서비스 추상화 | ||
final var userService = new TxUserService(appUserService, transactionTemplate); | ||
|
||
final User user = userDao.findById(1L).get(); | ||
final var oldPassword = user.getPassword(); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,25 +13,39 @@ public abstract class DataSourceUtils { | |
private DataSourceUtils() {} | ||
|
||
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connection != null) { | ||
return connection; | ||
if (TransactionSynchronizationManager.hasResource(dataSource)) { | ||
return TransactionSynchronizationManager.getResource(dataSource); | ||
} | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
Connection connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
} | ||
|
||
public static void releaseConnection(Connection connection, DataSource dataSource) { | ||
public static void releaseConnection(Connection connection) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); | ||
} | ||
} | ||
|
||
public static void closeNotTransactional(final DataSource dataSource, final Connection connection) { | ||
if (isTransactional(dataSource, connection)) { | ||
return; | ||
} | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
releaseConnection(connection); | ||
} | ||
|
||
private static boolean isTransactional(final DataSource dataSource, final Connection connection) { | ||
if (TransactionSynchronizationManager.hasResource(dataSource)) { | ||
return TransactionSynchronizationManager.getResource(dataSource) == connection; | ||
} | ||
return false; | ||
} | ||
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. R: 매번 true를 반환하는 것 같아요! TransactionTemplate을 사용하지 않는다면 커넥션을 영원히 종료하지 않는 문제가 생길 것 같아요! 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. Connection.getAutoCommit()을 통해 확인 후, close 하도록 수정했습니다! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.meta.When; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Nonnull(when = When.MAYBE) | ||
public @interface Nullable { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package org.springframework.transaction.support; | ||
|
||
@FunctionalInterface | ||
public interface TransactionCallback { | ||
public interface TransactionCallback<T> { | ||
|
||
void execute(); | ||
@Nullable | ||
T execute(); | ||
} |
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.
크.. 나머지 메서드들도 트랜잭션 다 깔끔하게 적용해주셨네요 👍