-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 4단계] 연어(황재현) 미션 제출합니다. (#577)
* feat: TransactionSynchronizationManager 구현 * refactor: 커넥션 관리 DataSourceUtils 사용하도록 변경 * refactor: DAO가 현재 스레드의 커넥션을 사용하도록 변경 * refactor: Application 로직만 있는 객체와 트랜잭션 관련 객체로 분리 * refactor: rollback의 역할 TransactionSynchronizationManager에게 위임 * refactor: close(), unbindResource() 같이 실행 되도록 변경 * refactor: rollback의 책임 TxUserService에게 위임
- Loading branch information
Showing
12 changed files
with
244 additions
and
102 deletions.
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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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 user = findById(id); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
|
||
public TxUserService(final UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@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) { | ||
final DataSource dataSource = DataSourceConfig.getInstance(); | ||
final Connection connection = DataSourceUtils.getConnection(dataSource); | ||
|
||
try { | ||
connection.setAutoCommit(false); | ||
userService.changePassword(id, newPassword, createBy); | ||
connection.commit(); | ||
} catch (final Exception e) { | ||
rollbackAndThrow(connection, e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
} | ||
|
||
private void rollbackAndThrow(final Connection connection, final Exception businessException) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(businessException); | ||
} catch (final SQLException rollbackException) { | ||
throw new DataAccessException(rollbackException); | ||
} | ||
} | ||
} |
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,52 +1,13 @@ | ||
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 org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.ConnectionManager; | ||
|
||
import java.sql.Connection; | ||
public interface UserService { | ||
|
||
public class 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; | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
|
||
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 ConnectionManager connectionManager = new ConnectionManager(DataSourceConfig.getInstance()); | ||
|
||
final Connection connection = connectionManager.getConnection(); | ||
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 (final Exception e) { | ||
connectionManager.rollback(connection); | ||
throw new DataAccessException(e); | ||
} finally { | ||
connectionManager.close(connection); | ||
} | ||
} | ||
} |
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
40 changes: 0 additions & 40 deletions
40
jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionManager.java
This file was deleted.
Oops, something went wrong.
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
48 changes: 42 additions & 6 deletions
48
.../main/java/org/springframework/transaction/support/TransactionSynchronizationManager.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,23 +1,59 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
public abstract class TransactionSynchronizationManager { | ||
|
||
private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>(); | ||
|
||
private TransactionSynchronizationManager() {} | ||
private TransactionSynchronizationManager() { | ||
} | ||
|
||
@Nullable | ||
public static Connection getResource(final DataSource key) { | ||
final Map<DataSource, Connection> connections = getConnections(); | ||
return connections.get(key); | ||
} | ||
|
||
private static Map<DataSource, Connection> getConnections() { | ||
Map<DataSource, Connection> connections = resources.get(); | ||
|
||
public static Connection getResource(DataSource key) { | ||
return null; | ||
if (isNull(connections)) { | ||
connections = new HashMap<>(); | ||
resources.set(connections); | ||
} | ||
|
||
return connections; | ||
} | ||
|
||
public static void bindResource(DataSource key, Connection value) { | ||
public static void bindResource(final DataSource key, final Connection value) { | ||
final Map<DataSource, Connection> connections = getConnections(); | ||
|
||
if (connections.containsKey(key)) { | ||
throw new IllegalStateException("이미 커넥션이 존재합니다."); | ||
} | ||
connections.put(key, value); | ||
} | ||
|
||
public static Connection unbindResource(DataSource key) { | ||
return null; | ||
public static Connection unbindResource(final DataSource key) { | ||
final Map<DataSource, Connection> connections = getConnections(); | ||
final Connection connection = connections.get(key); | ||
|
||
if (isNull(connection)) { | ||
throw new IllegalStateException("커넥션이 존재하지 않습니다."); | ||
} | ||
connections.remove(key); | ||
|
||
if (connections.isEmpty()) { | ||
TransactionSynchronizationManager.resources.remove(); | ||
} | ||
|
||
return connection; | ||
} | ||
} |
Oops, something went wrong.