-
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단계] 여우(조승현) 미션 제출합니다. #602
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f97f2f9
refactor: 3단계 피드백 반영 - null체크 책임을 service에서만 수행하기
BackFoxx d29b6ff
refactor: 3단계 피드백 반영 - 서비스 예외 발생시 DataAccessException 대신 RuntimeExcep…
BackFoxx dc40b02
refactor: 4단계 - DataSourceUtils를 구현하고 핵심기능 테스트하기
BackFoxx 084d773
refactor: 4단계 - DataSourceUtils를 활용해 DAO와 Service가 Connection을 의존하지 않…
BackFoxx 74acc10
refactor: 4단계 - JDK Dynamic Proxy를 사용해 UserService를 데이터베이스 관련 의존성과 완전…
BackFoxx 4a0ba70
refactor: 4단계 피드백 - TransactionProxyHandler에서 던지는 예외로 RuntimeExceptio…
BackFoxx 5e0c2ed
refactor: 4단계 피드백 - transaction 적용을 하지 않는 데이터베이스 작업은 실행 후 Connection …
BackFoxx 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
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
12 changes: 6 additions & 6 deletions
12
app/src/main/java/com/techcourse/repository/UserRepository.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,18 +1,18 @@ | ||
package com.techcourse.repository; | ||
|
||
import com.techcourse.domain.User; | ||
import java.sql.Connection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface UserRepository { | ||
|
||
void insert(final Connection connection, final User user); | ||
void insert(final User user); | ||
|
||
void update(final Connection connection, final User user); | ||
void update(final User user); | ||
|
||
List<User> findAll(final Connection connection); | ||
List<User> findAll(); | ||
|
||
User findById(final Connection connection, final Long id); | ||
Optional<User> findById(final Long id); | ||
|
||
User findByAccount(final Connection connection, final String account); | ||
Optional<User> findByAccount(final String account); | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/techcourse/service/TransactionProxyHandler.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,45 @@ | ||
package com.techcourse.service; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.SQLTransactionRollbackException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class TransactionProxyHandler implements InvocationHandler { | ||
|
||
private final Object target; | ||
private final DataSource dataSource; | ||
|
||
public TransactionProxyHandler(final Object target, final DataSource dataSource) { | ||
this.target = target; | ||
this.dataSource = dataSource; | ||
} | ||
|
||
@Override | ||
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { | ||
final Connection connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
final Object result = method.invoke(target, args); | ||
connection.commit(); | ||
|
||
return result; | ||
} catch (RuntimeException | SQLException e) { | ||
throw handleTransactionFailure(connection, e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
} | ||
Comment on lines
+21
to
+35
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. 오 프록시를 사용하셨군요! 덕분에 새로운 걸 배워갑니다. 🙇♀️ |
||
|
||
private Exception handleTransactionFailure(final Connection connection, final Exception e) { | ||
try { | ||
connection.rollback(); | ||
return e; | ||
} catch (SQLException ex) { | ||
return new SQLTransactionRollbackException(ex); | ||
} | ||
} | ||
} |
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,55 +1,11 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDaoWithJdbcTemplate; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.dao.SQLTransactionRollbackException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
User findById(final long id); | ||
|
||
private final UserDaoWithJdbcTemplate userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final Connection connection; | ||
void insert(final User user); | ||
|
||
public UserService(final Connection connection, final UserDaoWithJdbcTemplate userDao, final UserHistoryDao userHistoryDao) { | ||
this.connection = connection; | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(connection, id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(connection, user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
try { | ||
connection.setAutoCommit(false); | ||
|
||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (RuntimeException | SQLException e) { | ||
handleTransactionFailure(e); | ||
} | ||
} | ||
|
||
private void handleTransactionFailure(final Exception e) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} catch (SQLException ex) { | ||
throw new SQLTransactionRollbackException(ex); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/techcourse/service/UserServiceImpl.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,40 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDaoWithJdbcTemplate; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.util.Optional; | ||
|
||
public class UserServiceImpl implements UserService { | ||
|
||
private final UserDaoWithJdbcTemplate userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public UserServiceImpl(final UserDaoWithJdbcTemplate userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
final Optional<User> user = userDao.findById(id); | ||
if (user.isEmpty()) { | ||
throw new IllegalArgumentException("회원을 찾을 수 없음"); | ||
} | ||
return user.get(); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
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)); | ||
} | ||
} |
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.
상수화 굿 👍
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.
SonarLint가 이런 거 정말 잘 짚어주더라구요! 구구 짱 🙌🏻