-
Notifications
You must be signed in to change notification settings - Fork 379
[3단계 - Transaction 적용하기] 시소(이갱민) 미션 제출합니다. #1181
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
egaeng09
wants to merge
6
commits into
woowacourse:egaeng09
Choose a base branch
from
egaeng09:step3
base: egaeng09
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c6ecfc
chore: 테스트 @Disable 삭제
egaeng09 367ecbc
refactor: UserHistoryDao 형식 UserDao와 통일
egaeng09 2f6a179
feat: 동일한 Connection 객체 사용하여 트랜잭션 추가
egaeng09 3b4f4eb
refactor: rollback, close 메서드 분리
egaeng09 ab247bb
refactor: 공통 SQL문 분리
egaeng09 7655bff
style: 컨벤션 통일
egaeng09 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
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. 시소 이건 2중 public void changePassword(final long id, final String newPassword, final String createBy) {
try (Connection conn = dataSource.getConnection()) {
conn.setAutoCommit(false);
try {
final var user = findById(conn, id);
user.changePassword(newPassword);
userDao.update(conn, user);
userHistoryDao.log(conn, new UserHistory(user, createBy));
conn.commit();
} catch (Exception e) {
conn.rollback();
throw e;
}
} catch (DataAccessException | SQLException e) {
throw new DataAccessException("비밀번호 변경 실패", e);
}
} |
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,33 +1,83 @@ | ||
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 java.util.NoSuchElementException; | ||
import javax.sql.DataSource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class UserService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserService.class); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao, final DataSource dataSource) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id).orElseThrow(NoSuchElementException::new); | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id) | ||
.orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
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)); | ||
Connection connection = null; | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
connection.setAutoCommit(false); | ||
|
||
final var user = findById(connection, id); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (Exception e) { | ||
rollback(connection); | ||
throw new DataAccessException(e); | ||
} finally { | ||
closeConnection(connection); | ||
} | ||
} | ||
|
||
private User findById(final Connection connection, final long id) { | ||
return userDao.findById(connection, id) | ||
.orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
private void rollback(final Connection connection) { | ||
if (connection != null) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException e) { | ||
log.error("Failed to rollback transaction", e); | ||
} | ||
} | ||
} | ||
|
||
private void closeConnection(final Connection connection) { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException e) { | ||
log.error("Failed to close connection", e); | ||
} | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.