-
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단계] 마코(이규성) 미션 제출합니다. #594
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8044e1
feat: AppUserService와 TxUserService 분리
aak2075 d9b4e84
test: TxUserService 롤백 테스트
aak2075 968cc34
test: stage0 프록시 적용
aak2075 abaad70
test: stage1 ProxyFactoryBean 적용
aak2075 ca69326
test: stage2 스프링 AOP에 추가하기
aak2075 8ec1797
refactor: AppUserService 트랜잭션 제거
aak2075 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
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
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 var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
|
||
public TxUserService(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return TransactionTemplate.query(() -> userService.findById(id), DataSourceConfig.getInstance()); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
TransactionTemplate.execute(() -> userService.insert(user), DataSourceConfig.getInstance()); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
TransactionTemplate.execute( | ||
() -> userService.changePassword(id, newPassword, createBy), DataSourceConfig.getInstance() | ||
); | ||
} | ||
} |
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,55 +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.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import java.sql.SQLException; | ||
public interface UserService { | ||
|
||
public class UserService { | ||
User findById(final long id); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
void insert(final User user); | ||
|
||
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); | ||
|
||
final var dataSource = DataSourceConfig.getInstance(); | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
|
||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (SQLException e) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} catch (SQLException e2) { | ||
throw new DataAccessException(e2); | ||
} | ||
} finally { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
} | ||
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
47 changes: 47 additions & 0 deletions
47
jdbc/src/main/java/org/springframework/transaction/support/TransactionTemplate.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,47 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
import java.util.function.Supplier; | ||
|
||
public class TransactionTemplate { | ||
|
||
private TransactionTemplate() { | ||
} | ||
|
||
public static void execute(Runnable transactionCallback, DataSource dataSource) { | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
|
||
transactionCallback.run(); | ||
|
||
connection.commit(); | ||
} catch (SQLException e) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} catch (SQLException e2) { | ||
throw new DataAccessException(e2); | ||
} | ||
} finally { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
} | ||
|
||
public static <T> T query(Supplier<T> transactionCallback, DataSource dataSource) { | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
|
||
return transactionCallback.get(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,38 @@ | ||
package aop.stage0; | ||
|
||
import aop.DataAccessException; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
public class TransactionHandler implements InvocationHandler { | ||
|
||
private final PlatformTransactionManager transactionManager; | ||
|
||
private final Object target; | ||
|
||
public TransactionHandler(PlatformTransactionManager transactionManager, Object target) { | ||
this.transactionManager = transactionManager; | ||
this.target = target; | ||
} | ||
|
||
/** | ||
* @Transactional 어노테이션이 존재하는 메서드만 트랜잭션 기능을 적용하도록 만들어보자. | ||
*/ | ||
@Override | ||
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { | ||
return null; | ||
final var transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition()); | ||
|
||
try { | ||
final var ret = method.invoke(target, args); | ||
|
||
transactionManager.commit(transactionStatus); | ||
return ret; | ||
} catch (Exception e) { | ||
transactionManager.rollback(transactionStatus); | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
package aop.stage1; | ||
|
||
import aop.DataAccessException; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
|
||
/** | ||
* 어드바이스(advice). 부가기능을 담고 있는 클래스 | ||
*/ | ||
public class TransactionAdvice implements MethodInterceptor { | ||
|
||
private final PlatformTransactionManager platformTransactionManager; | ||
|
||
public TransactionAdvice(PlatformTransactionManager platformTransactionManager) { | ||
this.platformTransactionManager = platformTransactionManager; | ||
} | ||
|
||
@Override | ||
public Object invoke(final MethodInvocation invocation) throws Throwable { | ||
return null; | ||
final var transactionStatus = platformTransactionManager.getTransaction(new DefaultTransactionDefinition()); | ||
|
||
try { | ||
final var ret = invocation.proceed(); | ||
platformTransactionManager.commit(transactionStatus); | ||
return ret; | ||
} catch (Exception e) { | ||
platformTransactionManager.rollback(transactionStatus); | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} |
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
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.
여기에서도 템플릿 콜팩 패턴 사용!!!👍 매우 좋은 방법인 것 같아요!!!😊