-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 트랜잭션 매니저 생성 및 TxUserService에서 트랜잭션 중복 로직 제거
- Loading branch information
Showing
4 changed files
with
45 additions
and
24 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/techcourse/service/TransactionCallback.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,7 @@ | ||
package com.techcourse.service; | ||
|
||
@FunctionalInterface | ||
public interface TransactionCallback<T> { | ||
|
||
T execute(); | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/techcourse/service/TransactionManager.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,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
35
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 |
---|---|---|
@@ -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; | ||
}); | ||
} | ||
} |
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