-
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단계] 말랑(신동훈) 미션 제출합니다. #558
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
971a7f0
TransactionSynchronizationManager 작성
shin-mallang f0480cf
DataSourceUtils 작성
shin-mallang 6031428
JdbcTemplate에서 connection을 인자로 받지 않고 DataSourceUtils를 사용
shin-mallang 73ab509
UserService 추상화
shin-mallang 0edff56
UserService에 트랜잭션 적용
shin-mallang 770439a
TxTemplate을 통한 프로그래밍적 트랜잭션 관리
shin-mallang 9154149
RuntimeException 대신 DataAccessException 사용
shin-mallang 60d5a00
RuntimeException 대신 DataAccessException 사용
shin-mallang 8864d98
JdbcTemplate의 queryForObject,queryForList에서 커넥션 공유
shin-mallang ad8eeae
쓰레드로컬의 데이터 비우는 조건 추가
shin-mallang 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 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
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)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/techcourse/service/TxTemplateUserService.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,29 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TxTemplate; | ||
|
||
public class TxTemplateUserService implements UserService { | ||
|
||
private final UserService delegate; | ||
private final TxTemplate txTemplate; | ||
|
||
public TxTemplateUserService(UserService delegate, TxTemplate txTemplate) { | ||
this.delegate = delegate; | ||
this.txTemplate = txTemplate; | ||
} | ||
|
||
public User findById(final long id) { | ||
return delegate.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
delegate.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
txTemplate.execute(() -> { | ||
delegate.changePassword(id, newPassword, createBy); | ||
}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService delegate; | ||
|
||
public TxUserService(UserService delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public User findById(final long id) { | ||
return delegate.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
delegate.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
try (Connection conn = DataSourceUtils.getConnection(dataSource)) { | ||
try { | ||
conn.setAutoCommit(false); | ||
delegate.changePassword(id, newPassword, createBy); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
shin-mallang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,49 +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 java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
void insert(final User user); | ||
|
||
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) { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
try (Connection conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
conn.commit(); | ||
} catch (Exception e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
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
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
24 changes: 20 additions & 4 deletions
24
.../main/java/org/springframework/transaction/support/TransactionSynchronizationManager.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,23 +1,39 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import javax.sql.DataSource; | ||
|
||
public abstract class TransactionSynchronizationManager { | ||
|
||
private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>(); | ||
|
||
private TransactionSynchronizationManager() {} | ||
private TransactionSynchronizationManager() { | ||
} | ||
|
||
@Nullable | ||
public static Connection getResource(DataSource key) { | ||
return null; | ||
Map<DataSource, Connection> dataSourceConnectionMap = resources.get(); | ||
if (dataSourceConnectionMap == null) { | ||
return null; | ||
} | ||
return dataSourceConnectionMap.get(key); | ||
} | ||
|
||
public static void bindResource(DataSource key, Connection value) { | ||
resources.set(new HashMap<>() {{ | ||
put(key, value); | ||
}}); | ||
} | ||
|
||
public static Connection unbindResource(DataSource key) { | ||
return null; | ||
Map<DataSource, Connection> dataSourceConnectionMap = resources.get(); | ||
Connection remove = dataSourceConnectionMap.remove(key); | ||
if (dataSourceConnectionMap.isEmpty()) { | ||
resources.remove(); | ||
} | ||
return remove; | ||
} | ||
} |
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.
오홍 네이밍을 delegate로 하셨는데
어떨 때 이런 네이밍을 사용하는지 알려주실 수 있나요.? 처음 봐서요..!
구체적인 작업은 UserService가 하도록 위임한다는 뜻인가요?
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.
음 사실 TxUserService에서 실제 로직은 다른 서비스에 위임하기 때문에, 이를 표현하기 위해서 이렇게 네이밍 했습니다!