-
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 라이브러리 구현하기 - 3,4단계] 케로(김지현) 미션 제출합니다. #606
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b500ea2
study
jyeost e3f68c2
feat: UserHistoryDao 로직 추가
jyeost e98b1e6
feat: UserService에서 changePassword를 한 connection에서 처리할 수 있도록 기능을 구현함
jyeost b539625
feat: TransactionSynchronizationManager이정상 동작하도록 기능추가
jyeost 24a7537
feat: 서비스와 DAO에서 DataSourceUtils를 사용하여 Connection을 가져오도록 함.
jyeost 2347c91
feat: DataSourceUtils에서 오토 커밋일 경우 커넥션을 반납할 수 있도록 함.
jyeost 7ff7f19
refactor: JdbcTemplate의 반복을 줄이고,dao에서 커넥션을 보내지 않도록 수정함
jyeost 3990184
feat: userService 에서 커넥션 관리를 하지 않도록 TransactionTemplate을 만들음
jyeost 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
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
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; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@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 findUser = findById(id); | ||
findUser.changePassword(newPassword); | ||
userDao.update(findUser); | ||
userHistoryDao.log(new UserHistory(findUser, createBy)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final AppUserService userService; | ||
|
||
private final TransactionTemplate transactionTemplate; | ||
|
||
public TxUserService(final AppUserService userService, final TransactionTemplate transactionTemplate) { | ||
this.userService = userService; | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionTemplate.execute(() -> { | ||
userService.insert(user); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.execute(() -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
}); | ||
} | ||
} |
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,32 +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; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
User findById(final long id); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
void insert(final User user); | ||
|
||
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); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
|
@@ -23,48 +24,37 @@ public JdbcTemplate(final DataSource dataSource) { | |
} | ||
|
||
public int update(final String sql, final Object... parameters) { | ||
try (Connection conn = dataSource.getConnection(); | ||
PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters) | ||
) { | ||
log.debug("Execute Update - query : {}", sql); | ||
return pstmt.executeUpdate(); | ||
return executeQuery(sql, PreparedStatement::executeUpdate, parameters); | ||
} | ||
|
||
private <T> T executeQuery(final String sql, final PreparedStateExecutor<T> executor, final Object... parameters) { | ||
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. 잘 반영해 주셨네요! 훨씬 깔끔해진 것 같습니다 ㅎㅎ |
||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try (PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)) { | ||
log.debug("Execute - query : {}", sql); | ||
return executor.execute(pstmt); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
|
||
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... parameters) { | ||
try (Connection conn = dataSource.getConnection(); | ||
PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters); | ||
ResultSet rs = pstmt.executeQuery() | ||
) { | ||
log.debug("Execute Query -query : {}", sql); | ||
return SingleResult.makeSingleResultFrom(makeResults(rowMapper, rs)); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} | ||
return executeQuery(sql, preparedStatement -> SingleResult.makeSingleResultFrom(makeResults(rowMapper, preparedStatement)), parameters); | ||
} | ||
|
||
private <T> List<T> makeResults(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
private <T> List<T> makeResults(final RowMapper<T> rowMapper, final PreparedStatement preparedStatement) throws SQLException { | ||
List<T> list = new ArrayList<>(); | ||
while (rs.next()) { | ||
list.add(rowMapper.mapping(rs)); | ||
try (final ResultSet resultSet = preparedStatement.executeQuery()) { | ||
while (resultSet.next()) { | ||
list.add(rowMapper.mapping(resultSet)); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
public <T> List<T> queryForList(final String sql, final RowMapper<T> rowMapper) { | ||
try (Connection conn = dataSource.getConnection(); | ||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
ResultSet rs = pstmt.executeQuery() | ||
) { | ||
log.debug("Execute Query - query : {}", sql); | ||
return makeResults(rowMapper, rs); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} | ||
public <T> List<T> queryForList(final String sql, final RowMapper<T> rowMapper, final Object... parameters) { | ||
return executeQuery(sql, preparedStatement -> makeResults(rowMapper, preparedStatement), parameters); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
jdbc/src/main/java/org/springframework/jdbc/core/PreparedStateExecutor.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,9 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public interface PreparedStateExecutor<T> { | ||
|
||
T execute(final PreparedStatement preparedStatement) throws SQLException; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
jdbc/src/main/java/org/springframework/transaction/support/TransactionExecutor.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,7 @@ | ||
package org.springframework.transaction.support; | ||
|
||
@FunctionalInterface | ||
public interface TransactionExecutor<T> { | ||
|
||
T execute(); | ||
} |
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
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.
void 메서드마다 null을 반환하는 것이 불편하다면, TransactionTemplate에 void 타입을 반환하는 메서드를 하나 더 만들어도 좋을 거 같아요!
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.
그런 방법이 있군여...!!!
수정해보도록 하겠습니다 감사합니다 !!!