-
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단계] 우르(김현우) 미션 제출합니다. #550
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d15fc85
feat : 트랜잭션 서비스 추상화하기
java-saeng 2078f76
refactor : 트랜잭션 매니저가 여러 개의 DataSource를 가질 수 있도록 수정
java-saeng 44195f0
refactor : TransactionManager -> TransactionSynchronizationManager로 변경
java-saeng 0ccc687
refactor : TransactionSynchronizationManager에서 Connection은 DataSource…
java-saeng 1e5a709
refactor : CheckedException을 CustomException으로 전환
java-saeng 2836fac
refactor : release 시 connection 예외 처리
java-saeng 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
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
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; | ||
} | ||
|
||
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); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import javax.sql.DataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final DataSource dataSource; | ||
|
||
public TxUserService(final UserService userService, final DataSource dataSource) { | ||
this.userService = userService; | ||
this.dataSource = dataSource; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userService.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
TransactionSynchronizationManager.start(); | ||
|
||
try { | ||
userService.changePassword(id, newPassword, createBy); | ||
TransactionSynchronizationManager.commit(dataSource); | ||
} catch (Exception e) { | ||
TransactionSynchronizationManager.rollback(dataSource); | ||
throw e; | ||
} finally { | ||
TransactionSynchronizationManager.release(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,11 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.jdbc.core.TransactionManager; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
|
||
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); | ||
|
||
TransactionManager.start(); | ||
try { | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
TransactionManager.commit(); | ||
} catch (Exception e) { | ||
TransactionManager.rollback(); | ||
throw e; | ||
} finally { | ||
TransactionManager.release(); | ||
} | ||
} | ||
} |
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
87 changes: 0 additions & 87 deletions
87
jdbc/src/main/java/org/springframework/jdbc/core/TransactionManager.java
This file was deleted.
Oops, something went wrong.
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,5 +1,6 @@ | ||
package org.springframework.jdbc.datasource; | ||
|
||
import java.util.function.Function; | ||
import org.springframework.jdbc.CannotGetJdbcConnectionException; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
|
@@ -10,28 +11,29 @@ | |
// 4단계 미션에서 사용할 것 | ||
public abstract class DataSourceUtils { | ||
|
||
private DataSourceUtils() {} | ||
private DataSourceUtils() { | ||
} | ||
|
||
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connection != null) { | ||
return connection; | ||
} | ||
public static Connection getConnection(DataSource dataSource) { | ||
return TransactionSynchronizationManager.getResource(dataSource) | ||
.orElseGet(() -> createConnection(dataSource)); | ||
} | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
private static Connection createConnection(final DataSource dataSource) { | ||
try { | ||
final Connection connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
} | ||
Comment on lines
+22
to
+30
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. 세심하게 메서드 분리 잘 해주셨네요 😄 |
||
|
||
public static void releaseConnection(Connection connection, DataSource dataSource) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); | ||
} | ||
public static void releaseConnection(Connection connection) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
라이브러리를 사용하는 입장에서는 일일이 커넥션을 관리할 필요 없이 TransactionSynchronizationManager를 통해 편하게 트랜잭션 관리를 할 수 있게 되었네요! 👍