Skip to content

Commit

Permalink
refactor: 트랜잭션 매니저 생성 및 TxUserService에서 트랜잭션 중복 로직 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
swonny committed Oct 14, 2023
1 parent e63cb29 commit 05a153f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.techcourse.service;

@FunctionalInterface
public interface TransactionCallback<T> {

T execute();
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/techcourse/service/TransactionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.techcourse.service;

import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.sql.DataSource;

public class TransactionManager {

private final DataSource dataSource;

public TransactionManager(final DataSource dataSource) {
this.dataSource = dataSource;
}

public <T> T execute(final TransactionCallback<T> callback) {
TransactionSynchronizationManager.startNewTransaction(dataSource);

final T result = callback.execute();

TransactionSynchronizationManager.finishTransaction(dataSource);

return result;
}
}
35 changes: 12 additions & 23 deletions app/src/main/java/com/techcourse/service/TxUserService.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package com.techcourse.service;

import com.techcourse.domain.User;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.sql.DataSource;

public class TxUserService implements UserService {

private final DataSource dataSource;
private final AppUserService userService;
private final TransactionManager transactionManager;

public TxUserService(final DataSource dataSource, final AppUserService userService) {
this.dataSource = dataSource;
public TxUserService(final AppUserService userService, final TransactionManager transactionManager) {
this.userService = userService;
this.transactionManager = transactionManager;
}

@Override
public User findById(final long id) {
TransactionSynchronizationManager.startNewTransaction(dataSource);

final User findUser = userService.findById(id);

TransactionSynchronizationManager.finishTransaction(dataSource);

return findUser;
return transactionManager.execute(() -> userService.findById(id));
}

@Override
public void insert(final User user) {
TransactionSynchronizationManager.startNewTransaction(dataSource);

userService.insert(user);

TransactionSynchronizationManager.finishTransaction(dataSource);
transactionManager.execute(() -> {
userService.insert(user);
return null;
});
}

@Override
public void changePassword(final long id, final String newPassword, final String createBy) {
TransactionSynchronizationManager.startNewTransaction(dataSource);

userService.changePassword(id, newPassword, createBy);

TransactionSynchronizationManager.finishTransaction(dataSource);
transactionManager.execute(() -> {
userService.changePassword(id, newPassword, createBy);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void testTransactionRollback() {
// 애플리케이션 서비스
final var appUserService = new AppUserService(userDao, userHistoryDao);
// 트랜잭션 서비스 추상화
final var userService = new TxUserService(DataSourceConfig.getInstance(), appUserService);
final var transactionManager = new TransactionManager(DataSourceConfig.getInstance());
final var userService = new TxUserService(appUserService, transactionManager);

final var newPassword = "newPassword";
final var createBy = "gugu";
Expand Down

0 comments on commit 05a153f

Please sign in to comment.