-
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단계] 이리내(성채연) 미션 제출합니다. #588
Changes from all commits
974d28d
fbc0f6c
d08c11f
67417a1
24ff04a
309ca75
882c2f6
bd626c9
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,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 User 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.transaction.TransactionExecutor; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionExecutor transactionExecutor; | ||
|
||
public TxUserService(final UserService userService, final TransactionExecutor transactionExecutor) { | ||
this.userService = userService; | ||
this.transactionExecutor = transactionExecutor; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionExecutor.execute(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionExecutor.execute(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionExecutor.execute(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,10 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
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 javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
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 DataSource dataSource = DataSourceConfig.getInstance(); | ||
final Connection connection = getConnection(dataSource); | ||
try (connection) { | ||
connection.setAutoCommit(false); | ||
|
||
final User user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (SQLException | DataAccessException e) { | ||
rollback(connection); | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
private Connection getConnection(final DataSource dataSource) { | ||
try { | ||
return dataSource.getConnection(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
|
||
private void rollback(final Connection connection) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.springframework.jdbc; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import java.sql.Connection; | ||
|
||
public class ConnectionHolder { | ||
|
||
private Connection connection; | ||
private boolean transactionActive = false; | ||
|
||
public ConnectionHolder(final Connection connection) { | ||
this.connection = connection; | ||
Comment on lines
+6
to
+13
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. 코멘트에 남겨주신것처럼 커넥션 홀더를 적용해주셨네요! 감동1 |
||
} | ||
|
||
public void setTransactionActive(final boolean transactionActive) { | ||
this.transactionActive = transactionActive; | ||
} | ||
|
||
public boolean isTransactionActive() { | ||
return transactionActive; | ||
} | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
public boolean has(final Connection connection) { | ||
return this.connection == connection; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import java.util.List; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class JdbcTemplate { | ||
|
||
|
@@ -23,24 +24,13 @@ public int update(final String sql, final Object... args) { | |
|
||
private <T> T execute(final PreparedStatementCallback preparedStatementCallback, | ||
final ExecutionCallback<T> executionCallback) { | ||
try (final Connection connection = dataSource.getConnection()) { | ||
return executeWithConnection(connection, preparedStatementCallback, executionCallback); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
public int update(final Connection connection, final String sql, final Object... args) { | ||
return executeWithConnection(connection, conn -> prepareStatement(sql, conn, args), PreparedStatement::executeUpdate); | ||
} | ||
|
||
private <T> T executeWithConnection(final Connection connection, | ||
final PreparedStatementCallback preparedStatementCallback, | ||
final ExecutionCallback<T> executionCallback) { | ||
final Connection connection = DataSourceUtils.getConnection(dataSource); | ||
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. |
||
try (final PreparedStatement pstmt = preparedStatementCallback.prepareStatement(connection)) { | ||
return executionCallback.execute(pstmt); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package org.springframework.jdbc.datasource; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.CannotGetJdbcConnectionException; | ||
import org.springframework.jdbc.ConnectionHolder; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import javax.sql.DataSource; | ||
|
@@ -12,23 +16,59 @@ public abstract class DataSourceUtils { | |
|
||
private DataSourceUtils() {} | ||
|
||
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connection != null) { | ||
return connection; | ||
public static Connection getConnection(final DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
final ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connectionHolder != null) { | ||
return connectionHolder.getConnection(); | ||
} | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
final Connection connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
} | ||
|
||
public static void releaseConnection(Connection connection, DataSource dataSource) { | ||
public static void startTransaction(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
try{ | ||
connectionHolder.setTransactionActive(true); | ||
connection.setAutoCommit(false); | ||
}catch(SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
|
||
private static ConnectionHolder getConnectionHolder(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource); | ||
if(isNull(connectionHolder)) { | ||
throw new IllegalStateException(); | ||
} | ||
if(!connectionHolder.has(connection)) { | ||
throw new IllegalStateException(); | ||
} | ||
return connectionHolder; | ||
} | ||
|
||
public static void finishTransaction(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
try{ | ||
connection.commit(); | ||
connectionHolder.setTransactionActive(false); | ||
}catch(SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
|
||
public static void releaseConnection(final Connection connection, final DataSource dataSource) { | ||
try { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
if(connectionHolder.isTransactionActive()) { | ||
return; | ||
} | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
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(); 에서 예외가 발생하더라도 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. 저또한 예외가 발생하더라도 비워주는것이 현재코드로서는 안전하다는 생각이 드네요 |
||
connection.close(); | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); | ||
|
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.
캬 나머니 코드도 모두 트렌젝션을 적용해주셨군요 굳입니다