-
Notifications
You must be signed in to change notification settings - Fork 379
[4단계 - Transaction synchronization 적용하기] 한스(이현수) 미션 제출합니다. #1168
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
Open
20HyeonsuLee
wants to merge
1
commit into
woowacourse:20hyeonsulee
Choose a base branch
from
20HyeonsuLee:step4-new
base: 20hyeonsulee
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/techcourse/service/AppUserService.java
This file contains hidden or 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,33 @@ | ||
package com.techcourse.service; | ||
|
||
import com.interface21.dao.DataAccessException; | ||
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 DataAccessException("존재하지 않는 유저입니다.")); | ||
} | ||
|
||
public void save(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)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/techcourse/service/TxUserService.java
This file contains hidden or 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 com.techcourse.service; | ||
|
||
import com.interface21.dao.DataAccessException; | ||
import com.interface21.jdbc.datasource.DataSourceUtils; | ||
import com.interface21.transaction.support.TransactionSynchronizationManager; | ||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
|
||
public TxUserService(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
public void save(final User user) { | ||
userService.save(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final DataSource dataSource = DataSourceConfig.getInstance(); | ||
final Connection connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
userService.changePassword(id, newPassword, createBy); | ||
connection.commit(); | ||
} catch (Exception e) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
Comment on lines
+27
to
+46
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. P5: 이 코드의 종착지는 아시죠? AOP도 배워봤으니, 시간 되실때 어노테이션으로 구현해보는것도 추천드립니다. 이번주는 바쁘니 넘어가시죠 ㅎㅎ |
||
} |
This file contains hidden or 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,51 +1,12 @@ | ||
package com.techcourse.service; | ||
|
||
import com.interface21.dao.DataAccessException; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao, DataSource dataSource) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.dataSource = dataSource; | ||
} | ||
void save(final User user); | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id).orElseThrow(() -> new DataAccessException("존재하지 않는 유저입니다.")); | ||
} | ||
|
||
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); | ||
|
||
try (Connection connection = dataSource.getConnection()) { | ||
connection.setAutoCommit(false); | ||
try { | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (Exception e) { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createdBy); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
24 changes: 21 additions & 3 deletions
24
.../src/main/java/com/interface21/transaction/support/TransactionSynchronizationManager.java
This file contains hidden or 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,23 +1,41 @@ | ||
package com.interface21.transaction.support; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.sql.DataSource; | ||
|
||
public abstract class TransactionSynchronizationManager { | ||
|
||
private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>(); | ||
|
||
private TransactionSynchronizationManager() {} | ||
|
||
public static boolean hasConnection(DataSource dataSource) { | ||
if (resources.get() == null) { | ||
return false; | ||
} | ||
return resources.get().containsKey(dataSource); | ||
} | ||
|
||
public static Connection getResource(DataSource key) { | ||
return null; | ||
if (resources.get() == null) { | ||
return null; | ||
} | ||
return resources.get().get(key); | ||
} | ||
|
||
public static void bindResource(DataSource key, Connection value) { | ||
if (resources.get() == null) { | ||
resources.set(new HashMap<>()); | ||
} | ||
Comment on lines
+29
to
+31
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. P5: LocalThread에는 최초 선언시 값을 초기화 할 수 있는 함수가 있는 것 같아요. 대신 사용해보면 어떨까요?? |
||
resources.get().put(key, value); | ||
} | ||
|
||
public static Connection unbindResource(DataSource key) { | ||
return null; | ||
if (resources.get() == null) { | ||
throw new IllegalStateException("unbind할 리소스가 존재하지 않습니다."); | ||
} | ||
return resources.get().remove(key); | ||
} | ||
} |
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.
P2: 이 부분에서 rollback 시 예외가 발생한다면 원본 예외(Exception e)가 덮어 씌워지는데 어떤가요? 만약
changePassword
에서 NPE가 발생하고, 롤백 시 예외가 발생한다면 NPE는 알 수 없게 될 것 같아요.