-
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단계] 리오(오영택) 미션 제출합니다. #574
Changes from 6 commits
2785499
275bd89
a61dae9
c13f3d6
eac3a30
40a783e
7ef6aed
f0104fc
692ec95
d44941f
72752e2
f5ba5dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
public class MockUserHistoryDao extends UserHistoryDao { | ||
|
@@ -13,6 +13,7 @@ public MockUserHistoryDao(final JdbcTemplate jdbcTemplate) { | |
|
||
@Override | ||
public void log(final UserHistory userHistory) { | ||
System.out.println("asdf"); | ||
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. 앗 ㅎㅎ 🙈 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. 앗! 이대로 머지가 되다니... 슬프군요... 죄송합니다.... 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. 앗.. 이럴 수가.. DM으로 의견 여쭤보고 머지할걸 그랬네용.. 저도 죄송합니다 🥲 |
||
throw new DataAccessException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.CannotGetJdbcConnectionException; | ||
import org.springframework.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
|
@@ -25,13 +25,15 @@ class TxUserServiceTest { | |
@BeforeEach | ||
void setUp() { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
|
||
DatabasePopulatorUtils.execute(dataSource); | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
this.transactionTemplate = new TransactionTemplate(dataSource); | ||
|
||
DatabasePopulatorUtils.execute(dataSource); | ||
userDao.deleteAll(); | ||
|
||
|
||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
this.user = userDao.findByAccount("gugu"); | ||
|
@@ -46,8 +48,8 @@ void testTransactionRollback() { | |
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
|
||
assertThrows(CannotGetJdbcConnectionException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)); | ||
assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(user.getId(), newPassword, createBy)); | ||
|
||
final var actual = userService.findById(user.getId()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,11 @@ | |
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
@@ -25,25 +22,27 @@ public JdbcTemplate(DataSource dataSource) { | |
} | ||
|
||
public void update(String sql, Object... values) { | ||
try ( | ||
Connection connection = dataSource.getConnection(); | ||
PreparedStatement pstmt = connection.prepareStatement(sql); | ||
) { | ||
log.debug("query : {}", sql); | ||
Connection connection = DataSourceUtils.getConnection(dataSource); | ||
Jaeyoung22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try { | ||
PreparedStatement pstmt = connection.prepareStatement(sql); | ||
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. PreparedStatement는 Connection에 의해 자동으로 닫히는 부분이 아닌 것으로 알고 있어요! 디버깅을 찍어보았을 때,
인 것 같아요!! PreparedStatement는 이전 코드처럼 닫아주면 어떨까요?.? 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. 앗 그렇군요... 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. 헉 어쩌면 제가 디버깅을 잘못한 걸 수도...! 요건 저도 더 확인해볼게요!! |
||
log.debug("query : {}", sql); | ||
setValues(pstmt, values); | ||
pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection); | ||
} | ||
} | ||
|
||
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... values) { | ||
try ( | ||
Connection connection = dataSource.getConnection(); | ||
PreparedStatement pstmt = connection.prepareStatement(sql); | ||
) { | ||
Connection connection = DataSourceUtils.getConnection(dataSource); | ||
|
||
try { | ||
PreparedStatement pstmt = connection.prepareStatement(sql); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
setValues(pstmt, values); | ||
|
@@ -57,6 +56,8 @@ public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... values) { | |
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection); | ||
} | ||
} | ||
|
||
|
@@ -74,16 +75,19 @@ public <T> Optional<T> queryForObject(String sql, RowMapper<T> rowMapper, Object | |
} | ||
|
||
public void execute(String sql) { | ||
try ( | ||
Connection connection = dataSource.getConnection(); | ||
Statement stmt = connection.createStatement(); | ||
) { | ||
Connection connection = DataSourceUtils.getConnection(dataSource); | ||
|
||
try { | ||
Statement stmt = connection.createStatement(); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
stmt.execute(sql); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
import org.springframework.DataAccessException; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
|
@@ -16,23 +15,23 @@ public TransactionTemplate(DataSource dataSource) { | |
} | ||
|
||
public void execute(Runnable runnable) { | ||
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. 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. 허허 이번 미션을 너무 쉽게 생각했군요.... 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. 멋지게 수정하셨군요 👍 👍 최곱니다~~~~~ 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. 중요하지는 않은 내용이지만, release를 finally 블럭에서 처리하도록 합칠 수 있지 않을까 싶기도 하네용! 리오 : |
||
Connection connection = DataSourceUtils.getConnection(dataSource); | ||
begin(connection); | ||
Connection connection = begin(); | ||
try { | ||
runnable.run(); | ||
|
||
commit(connection); | ||
} catch (Exception e) { | ||
rollback(connection); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
|
||
private void begin(Connection connection) { | ||
private Connection begin() { | ||
try { | ||
TransactionSynchronizationManager.setInTransaction(true); | ||
Connection connection = dataSource.getConnection(); | ||
connection.setAutoCommit(false); | ||
TransactionSynchronizationManager.bindConnection(dataSource, connection); | ||
return connection; | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
|
@@ -41,6 +40,7 @@ private void begin(Connection connection) { | |
private void commit(Connection connection) { | ||
try { | ||
connection.commit(); | ||
release(connection); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
|
@@ -49,6 +49,17 @@ private void commit(Connection connection) { | |
private void rollback(Connection connection) { | ||
try { | ||
connection.rollback(); | ||
release(connection); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
private void release(Connection connection) { | ||
try { | ||
TransactionSynchronizationManager.setInTransaction(false); | ||
Connection unbindedConnection = TransactionSynchronizationManager.unbindConnection(connection, dataSource); | ||
unbindedConnection.close(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
|
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.
findById
는 TransactionTemplate 으로 실행시키지 않으신 이유가 궁금해요~!!AppUserService의
changePassword
내에서findById
를 호출하는 걸 보면,findById
도 트랜잭션 내에서 실행시켜줘야 하지 않을까 싶은데 리오는 어떻게 생각하시나요?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.
그렇네요 제가 readonly=False의 느낌으로 트랜잭션을 설정했었는데, 바론 말대로 한 트랜잭션 내에서 진행이 되어야 겠군요... 감사합니다!