Skip to content
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 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/src/main/java/com/techcourse/service/AppUserService.java
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 app/src/main/java/com/techcourse/service/TxUserService.java
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()
);
}
}
51 changes: 4 additions & 47 deletions app/src/main/java/com/techcourse/service/UserService.java
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);
}
6 changes: 4 additions & 2 deletions app/src/test/java/com/techcourse/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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";
Expand All @@ -46,7 +46,9 @@ 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);


final var newPassword = "newPassword";
final var createBy = "gugu";
Expand Down
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);
}
}
Comment on lines +15 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에서도 템플릿 콜팩 패턴 사용!!!👍 매우 좋은 방법인 것 같아요!!!😊


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);
}
}
}
14 changes: 12 additions & 2 deletions study/src/test/java/aop/stage0/Stage0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.PlatformTransactionManager;

import java.lang.reflect.Proxy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
Expand Down Expand Up @@ -45,7 +47,11 @@ void setUp() {
@Test
void testChangePassword() {
final var appUserService = new AppUserService(userDao, userHistoryDao);
final UserService userService = null;
final UserService userService = (UserService) Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[] {UserService.class},
new TransactionHandler(platformTransactionManager, appUserService)
);

final var newPassword = "qqqqq";
final var createBy = "gugu";
Expand All @@ -59,7 +65,11 @@ void testChangePassword() {
@Test
void testTransactionRollback() {
final var appUserService = new AppUserService(userDao, stubUserHistoryDao);
final UserService userService = null;
final UserService userService = (UserService) Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[] {UserService.class},
new TransactionHandler(platformTransactionManager, appUserService)
);

final var newPassword = "newPassword";
final var createBy = "gugu";
Expand Down
25 changes: 24 additions & 1 deletion study/src/test/java/aop/stage0/TransactionHandler.java
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);
}
}
}
25 changes: 23 additions & 2 deletions study/src/test/java/aop/stage1/Stage1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.PlatformTransactionManager;
Expand Down Expand Up @@ -41,7 +42,17 @@ void setUp() {

@Test
void testChangePassword() {
final UserService userService = null;
final var target = new UserService(userDao, userHistoryDao);

final var proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTarget(target);

proxyFactoryBean.setProxyTargetClass(true);

final var pointcut = new TransactionPointcut();
final var advice = new TransactionAdvice(platformTransactionManager);
proxyFactoryBean.addAdvisor(new TransactionAdvisor(pointcut, advice));
final UserService userService = (UserService) proxyFactoryBean.getObject();

final var newPassword = "qqqqq";
final var createBy = "gugu";
Expand All @@ -54,7 +65,17 @@ void testChangePassword() {

@Test
void testTransactionRollback() {
final UserService userService = null;
final var target = new UserService(userDao, stubUserHistoryDao);

final var proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTarget(target);

proxyFactoryBean.setProxyTargetClass(true);

final var pointcut = new TransactionPointcut();
final var advice = new TransactionAdvice(platformTransactionManager);
proxyFactoryBean.addAdvisor(new TransactionAdvisor(pointcut, advice));
final UserService userService = (UserService) proxyFactoryBean.getObject();

final var newPassword = "newPassword";
final var createBy = "gugu";
Expand Down
20 changes: 19 additions & 1 deletion study/src/test/java/aop/stage1/TransactionAdvice.java
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);
}
}
}
13 changes: 11 additions & 2 deletions study/src/test/java/aop/stage1/TransactionAdvisor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@
*/
public class TransactionAdvisor implements PointcutAdvisor {

private final Pointcut pointcut;

private final Advice advice;

public TransactionAdvisor(Pointcut pointcut, Advice advice) {
this.pointcut = pointcut;
this.advice = advice;
}

@Override
public Pointcut getPointcut() {
return null;
return pointcut;
}

@Override
public Advice getAdvice() {
return null;
return advice;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion study/src/test/java/aop/stage1/TransactionPointcut.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aop.stage1;

import aop.Transactional;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

import java.lang.reflect.Method;
Expand All @@ -14,6 +15,6 @@ public class TransactionPointcut extends StaticMethodMatcherPointcut {

@Override
public boolean matches(final Method method, final Class<?> targetClass) {
return false;
return method.isAnnotationPresent(Transactional.class);
}
}
Loading