-
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단계] 블랙캣(송우석) 미션 제출합니다🚀 #580
Changes from all commits
756567f
ea13b73
aa129d5
6153ac8
dd109c2
099cbe5
dcbf0e1
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.jdbc.support.TransactionTemplate; | ||
|
||
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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.jdbc.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
public TxUserService(final UserService 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) { | ||
userService.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.execute(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,13 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.jdbc.support.TransactionTemplate; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final TransactionTemplate transactionTemplate; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao, final TransactionTemplate transactionTemplate) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
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) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
|
||
transactionTemplate.execute(() -> { | ||
updateUser(user); | ||
writeUserHistory(new UserHistory(user, createBy)); | ||
}); | ||
} | ||
|
||
private void updateUser(final User user) { | ||
userDao.update(user); | ||
} | ||
|
||
private void writeUserHistory(final UserHistory userHistory) { | ||
userHistoryDao.log(userHistory); | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,22 @@ | ||
package org.springframework.jdbc.datasource; | ||
|
||
import java.sql.Connection; | ||
import javax.sql.DataSource; | ||
import org.springframework.jdbc.CannotGetJdbcConnectionException; | ||
import org.springframework.jdbc.support.ConnectionHolder; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
// 4단계 미션에서 사용할 것 | ||
public abstract class DataSourceUtils { | ||
|
||
private DataSourceUtils() {} | ||
|
||
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connection != null) { | ||
return connection; | ||
} | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
private DataSourceUtils() { | ||
} | ||
|
||
public static void releaseConnection(Connection connection, DataSource dataSource) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); | ||
public static ConnectionHolder getConnectionHolder(DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
final Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (TransactionSynchronizationManager.isTransactionBegan()) { | ||
return ConnectionHolder.activeTransaction(connection); | ||
} | ||
|
||
return ConnectionHolder.disableTransaction(connection); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
package org.springframework.jdbc.support; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class ConnectionHolder implements AutoCloseable { | ||
|
||
private static final int QUERY_PARAMETER_STEP = 1; | ||
|
||
private final Connection connection; | ||
private final boolean isTransactionActive; | ||
|
||
|
@@ -16,16 +12,6 @@ private ConnectionHolder(final Connection connection, final boolean isTransactio | |
this.isTransactionActive = isTransactionActive; | ||
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으로 말씀해주셨던 부분인데 궁금한 점이 생겨 질문 드려요!
저는 블랙캣이 해당 부분을 해소하기 위해 ConnectionHolder에서 AutoCloseable을 구현하셨다고 생각했습니다. |
||
} | ||
|
||
public PreparedStatement createPrepareStatement(final String sql, final Object[] parameters) throws SQLException { | ||
final PreparedStatement preparedStatement = connection.prepareStatement(sql); | ||
|
||
for (int index = 0; index < parameters.length; index++) { | ||
preparedStatement.setObject(index + QUERY_PARAMETER_STEP, parameters[index]); | ||
} | ||
|
||
return preparedStatement; | ||
} | ||
|
||
public static ConnectionHolder activeTransaction(final Connection connection) { | ||
return new ConnectionHolder(connection, true); | ||
} | ||
|
@@ -34,6 +20,10 @@ public static ConnectionHolder disableTransaction(final Connection connection) { | |
return new ConnectionHolder(connection, false); | ||
} | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
if (!isTransactionActive) { | ||
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. 여기에서 connection을 close하기 전 autoCommit을 true로 변경해주어야 합니다! |
||
|
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.
지난번에 고민하셨던 부분이었던 것 같은데 jdbcTemplate으로 옮겨주셨네요!
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.
저번에 밀리의 리뷰를 들어보고 고민해보니 아무래도
jdbcTemplate
가 더 맞는거 같아서 복구 시켜놓았습니다!