From e84424a64609d16589224ee7903608b7aa3ea8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Sun, 8 Oct 2023 18:50:44 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20DataSourceUtils,=20TxSyncManage?= =?UTF-8?q?r=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../transaction/TransactionManager.java | 24 +++++++++---------- .../TransactionSynchronizationManager.java | 7 ++++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java index 30393f76c2..d28f758228 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java @@ -1,6 +1,7 @@ package org.springframework.transaction; -import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.datasource.DataSourceUtils; +import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.sql.Connection; @@ -8,18 +9,12 @@ public class TransactionManager { + private final DataSource dataSource; private final Connection connection; public TransactionManager(DataSource dataSource) { - try { - this.connection = dataSource.getConnection(); - } catch (SQLException e) { - throw new DataAccessException(e); - } - } - - public TransactionManager(Connection connection) { - this.connection = connection; + this.dataSource = dataSource; + this.connection = DataSourceUtils.getConnection(dataSource); } public void begin() throws SQLException { @@ -28,15 +23,20 @@ public void begin() throws SQLException { public void commit() throws SQLException { connection.commit(); - connection.close(); + this.close(); } public void rollback() throws SQLException { connection.rollback(); - connection.close(); + this.close(); } public Connection getConnection() { return connection; } + + public void close() { + DataSourceUtils.releaseConnection(connection, dataSource); + TransactionSynchronizationManager.unbindResource(dataSource); + } } diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 715557fc66..103f4d7249 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -11,13 +11,16 @@ public abstract class TransactionSynchronizationManager { private TransactionSynchronizationManager() {} public static Connection getResource(DataSource key) { - return null; + return resources.get().getOrDefault(key, null); } public static void bindResource(DataSource key, Connection value) { + resources.get().put(key, value); } public static Connection unbindResource(DataSource key) { - return null; + Connection released = resources.get().remove(key); + resources.remove(); + return released; } } From 35985c21d02f6a01f0ee6fe39f310aa789f947d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Sun, 8 Oct 2023 20:48:31 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20UserService=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/service/AppUserService.java | 35 ++++++++++++++++++ .../com/techcourse/service/TxUserService.java | 30 ++++++++++++++++ .../com/techcourse/service/UserService.java | 36 +++---------------- .../techcourse/service/UserServiceTest.java | 8 ++--- .../transaction/TransactionExecutor.java | 24 +++++++++++-- 5 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/com/techcourse/service/AppUserService.java create mode 100644 app/src/main/java/com/techcourse/service/TxUserService.java diff --git a/app/src/main/java/com/techcourse/service/AppUserService.java b/app/src/main/java/com/techcourse/service/AppUserService.java new file mode 100644 index 0000000000..9ca0dc95fb --- /dev/null +++ b/app/src/main/java/com/techcourse/service/AppUserService.java @@ -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(UserDao userDao, UserHistoryDao userHistoryDao) { + this.userDao = userDao; + this.userHistoryDao = userHistoryDao; + } + + @Override + public User findById(long id) { + return userDao.findById(id); + } + + @Override + public void insert(User user) { + userDao.insert(user); + } + + @Override + public void changePassword(long id, String newPassword, String createBy) { + final var user = findById(id); + user.changePassword(newPassword); + userDao.update(user); + userHistoryDao.log(new UserHistory(user, createBy)); + } +} diff --git a/app/src/main/java/com/techcourse/service/TxUserService.java b/app/src/main/java/com/techcourse/service/TxUserService.java new file mode 100644 index 0000000000..2879b4436d --- /dev/null +++ b/app/src/main/java/com/techcourse/service/TxUserService.java @@ -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(UserService userService, TransactionExecutor transactionExecutor) { + this.userService = userService; + this.transactionExecutor = transactionExecutor; + } + + @Override + public User findById(long id) { + return transactionExecutor.executeWithResult(() -> userService.findById(id)); + } + + @Override + public void insert(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)); + } +} diff --git a/app/src/main/java/com/techcourse/service/UserService.java b/app/src/main/java/com/techcourse/service/UserService.java index abd9c782fa..42d01bf760 100644 --- a/app/src/main/java/com/techcourse/service/UserService.java +++ b/app/src/main/java/com/techcourse/service/UserService.java @@ -1,38 +1,10 @@ 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.transaction.TransactionExecutor; -import org.springframework.transaction.TransactionManager; -public class UserService { +public interface UserService { - private final UserDao userDao; - private final UserHistoryDao userHistoryDao; - private final TransactionManager transactionManager; - - public UserService(final TransactionManager transactionManager, final UserDao userDao, final UserHistoryDao userHistoryDao) { - this.userDao = userDao; - this.userHistoryDao = userHistoryDao; - this.transactionManager = transactionManager; - } - - 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) { - TransactionExecutor.execute(transactionManager, () -> { - final var user = findById(id); - user.changePassword(newPassword); - userDao.update(transactionManager.getConnection(), user); - userHistoryDao.log(transactionManager.getConnection(), new UserHistory(user, createBy)); - }); - } + User findById(final long id); + void insert(final User user); + void changePassword(final long id, final String newPassword, final String createBy); } diff --git a/app/src/test/java/com/techcourse/service/UserServiceTest.java b/app/src/test/java/com/techcourse/service/UserServiceTest.java index ed2d431fff..d12c40b565 100644 --- a/app/src/test/java/com/techcourse/service/UserServiceTest.java +++ b/app/src/test/java/com/techcourse/service/UserServiceTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.TransactionExecutor; import org.springframework.transaction.TransactionManager; import static org.assertj.core.api.Assertions.assertThat; @@ -34,7 +35,7 @@ void setUp() { @Test void testChangePassword() { final var userHistoryDao = new UserHistoryDao(jdbcTemplate); - final var userService = new UserService(transactionManager, userDao, userHistoryDao); + final var userService = new AppUserService(userDao, userHistoryDao); final var newPassword = "qqqqq"; final var createBy = "gugu"; @@ -47,13 +48,12 @@ void testChangePassword() { @Test void testTransactionRollback() { - // 트랜잭션 롤백 테스트를 위해 mock으로 교체 final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); - final var userService = new UserService(transactionManager, userDao, userHistoryDao); + final var appUserService = new AppUserService(userDao, userHistoryDao); + final var userService = new TxUserService(appUserService, new TransactionExecutor(transactionManager)); final var newPassword = "newPassword"; final var createBy = "gugu"; - // 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. assertThrows(DataAccessException.class, () -> userService.changePassword(1L, newPassword, createBy)); diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java index 6ef0307197..5fa2712e10 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java @@ -3,13 +3,17 @@ import org.springframework.dao.DataAccessException; import java.sql.SQLException; +import java.util.function.Supplier; public class TransactionExecutor { - private TransactionExecutor() { + private final TransactionManager transactionManager; + + public TransactionExecutor(TransactionManager transactionManager) { + this.transactionManager = transactionManager; } - public static void execute(final TransactionManager transactionManager, final Runnable runnable) { + public void execute(final Runnable runnable) { try { transactionManager.begin(); runnable.run(); @@ -23,4 +27,20 @@ public static void execute(final TransactionManager transactionManager, final Ru throw new DataAccessException(e); } } + + public R executeWithResult(final Supplier supplier) { + try { + transactionManager.begin(); + R result = supplier.get(); + transactionManager.commit(); + return result; + } catch (Exception e) { + try { + transactionManager.rollback(); + } catch (SQLException ex) { + throw new DataAccessException(ex); + } + throw new DataAccessException(e); + } + } } From 943a0ae6d3b150073141d047c3262f83f219c15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Sun, 8 Oct 2023 21:26:56 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20TxManager=20UnChecked=20Excepti?= =?UTF-8?q?on=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../transaction/TransactionExecutor.java | 13 ++------- .../transaction/TransactionManager.java | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java index 5fa2712e10..05e9e0620f 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java @@ -2,7 +2,6 @@ import org.springframework.dao.DataAccessException; -import java.sql.SQLException; import java.util.function.Supplier; public class TransactionExecutor { @@ -19,11 +18,7 @@ public void execute(final Runnable runnable) { runnable.run(); transactionManager.commit(); } catch (Exception e) { - try { - transactionManager.rollback(); - } catch (SQLException ex) { - throw new DataAccessException(ex); - } + transactionManager.rollback(); throw new DataAccessException(e); } } @@ -35,11 +30,7 @@ public R executeWithResult(final Supplier supplier) { transactionManager.commit(); return result; } catch (Exception e) { - try { - transactionManager.rollback(); - } catch (SQLException ex) { - throw new DataAccessException(ex); - } + transactionManager.rollback(); throw new DataAccessException(e); } } diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java index d28f758228..73898370fe 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java @@ -1,5 +1,6 @@ package org.springframework.transaction; +import org.springframework.dao.DataAccessException; import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.transaction.support.TransactionSynchronizationManager; @@ -17,18 +18,30 @@ public TransactionManager(DataSource dataSource) { this.connection = DataSourceUtils.getConnection(dataSource); } - public void begin() throws SQLException { - connection.setAutoCommit(false); + public void begin() { + try { + connection.setAutoCommit(false); + } catch (SQLException e) { + throw new DataAccessException(e); + } } - public void commit() throws SQLException { - connection.commit(); - this.close(); + public void commit() { + try { + connection.commit(); + this.close(); + } catch (SQLException e) { + throw new DataAccessException(e); + } } - public void rollback() throws SQLException { - connection.rollback(); - this.close(); + public void rollback() { + try { + connection.rollback(); + this.close(); + } catch (SQLException e) { + throw new DataAccessException(e); + } } public Connection getConnection() { From 14d433675b3a61ca755d6df2b556fdb9389b5111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Tue, 10 Oct 2023 19:37:33 +0900 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20null=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TransactionSynchronizationManager.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 103f4d7249..5a592ff007 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -2,24 +2,39 @@ import javax.sql.DataSource; import java.sql.Connection; +import java.util.HashMap; import java.util.Map; public abstract class TransactionSynchronizationManager { private static final ThreadLocal> resources = new ThreadLocal<>(); - private TransactionSynchronizationManager() {} + private TransactionSynchronizationManager() { + } public static Connection getResource(DataSource key) { - return resources.get().getOrDefault(key, null); + Map connectionWithDataSource = resources.get(); + if (connectionWithDataSource == null) { + return null; + } + return connectionWithDataSource.getOrDefault(key, null); } public static void bindResource(DataSource key, Connection value) { - resources.get().put(key, value); + Map connectionWithDataSource = resources.get(); + if (connectionWithDataSource == null) { + connectionWithDataSource = new HashMap<>(); + resources.set(connectionWithDataSource); + } + connectionWithDataSource.put(key, value); } public static Connection unbindResource(DataSource key) { - Connection released = resources.get().remove(key); + Map connectionWithDataSource = resources.get(); + if (connectionWithDataSource == null) { + return null; + } + Connection released = connectionWithDataSource.remove(key); resources.remove(); return released; } From 305a6b4de50694b87fb855b2d8b16749157a4d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Tue, 10 Oct 2023 19:56:30 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20=EB=A1=9C=EC=A7=81=20release=20?= =?UTF-8?q?Connection=EC=9C=BC=EB=A1=9C=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/springframework/jdbc/datasource/DataSourceUtils.java | 1 + .../java/org/springframework/transaction/TransactionManager.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index 3c40bfec52..e225b38980 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -30,6 +30,7 @@ public static Connection getConnection(DataSource dataSource) throws CannotGetJd public static void releaseConnection(Connection connection, DataSource dataSource) { try { connection.close(); + TransactionSynchronizationManager.unbindResource(dataSource); } catch (SQLException ex) { throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); } diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java index 73898370fe..82af58be59 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java @@ -50,6 +50,5 @@ public Connection getConnection() { public void close() { DataSourceUtils.releaseConnection(connection, dataSource); - TransactionSynchronizationManager.unbindResource(dataSource); } } From 481b25421bd9dfe441a39d1d08ce07e1e8e9dab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Tue, 10 Oct 2023 20:17:32 +0900 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20Tx=20=EC=A2=85=EB=A3=8C=20?= =?UTF-8?q?=EC=8B=9C=EC=97=90=EB=8F=84=20connection=20=EB=82=A8=EA=B2=A8?= =?UTF-8?q?=EB=91=90=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/springframework/jdbc/core/JdbcTemplate.java | 3 ++- .../org/springframework/transaction/TransactionManager.java | 6 ++++-- .../support/TransactionSynchronizationManager.java | 4 +--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index b2aee014ec..0ce25aaee8 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -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.annotation.Nullable; import javax.sql.DataSource; @@ -48,8 +49,8 @@ private T execute(String sql, Connection connection, PrepareStatementExecuto } private T execute(String sql, PrepareStatementExecutor executor, Object... args) { + Connection connection = DataSourceUtils.getConnection(dataSource); try ( - Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args) ) { return executor.execute(preparedStatement); diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java index 82af58be59..6551bb2d0f 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java @@ -2,7 +2,6 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.sql.Connection; @@ -11,7 +10,7 @@ public class TransactionManager { private final DataSource dataSource; - private final Connection connection; + private Connection connection; public TransactionManager(DataSource dataSource) { this.dataSource = dataSource; @@ -20,6 +19,9 @@ public TransactionManager(DataSource dataSource) { public void begin() { try { + if (connection.isClosed()) { + connection = DataSourceUtils.getConnection(dataSource); + } connection.setAutoCommit(false); } catch (SQLException e) { throw new DataAccessException(e); diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 5a592ff007..05ed65e14c 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -34,8 +34,6 @@ public static Connection unbindResource(DataSource key) { if (connectionWithDataSource == null) { return null; } - Connection released = connectionWithDataSource.remove(key); - resources.remove(); - return released; + return connectionWithDataSource.remove(key); } } From 0355d74695ae269bcc8e4a8c668595723f5b4f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Tue, 10 Oct 2023 21:16:47 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20Tx=20=EC=8B=A4=ED=96=89=20?= =?UTF-8?q?=EC=8B=9C=20Manager=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20?= =?UTF-8?q?=EA=B0=9C=EB=B3=84=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/service/UserServiceTest.java | 3 +-- .../springframework/transaction/TransactionExecutor.java | 9 ++++++--- .../springframework/transaction/TransactionManager.java | 5 +---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/test/java/com/techcourse/service/UserServiceTest.java b/app/src/test/java/com/techcourse/service/UserServiceTest.java index d12c40b565..3713c3bb70 100644 --- a/app/src/test/java/com/techcourse/service/UserServiceTest.java +++ b/app/src/test/java/com/techcourse/service/UserServiceTest.java @@ -25,7 +25,6 @@ class UserServiceTest { void setUp() { this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); this.userDao = new UserDao(jdbcTemplate); - this.transactionManager = new TransactionManager(jdbcTemplate.getDataSource()); DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); final var user = new User("gugu", "password", "hkkang@woowahan.com"); @@ -50,7 +49,7 @@ void testChangePassword() { void testTransactionRollback() { final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); final var appUserService = new AppUserService(userDao, userHistoryDao); - final var userService = new TxUserService(appUserService, new TransactionExecutor(transactionManager)); + final var userService = new TxUserService(appUserService, new TransactionExecutor(jdbcTemplate.getDataSource())); final var newPassword = "newPassword"; final var createBy = "gugu"; diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java index 05e9e0620f..616728bb45 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionExecutor.java @@ -2,17 +2,19 @@ import org.springframework.dao.DataAccessException; +import javax.sql.DataSource; import java.util.function.Supplier; public class TransactionExecutor { - private final TransactionManager transactionManager; + private final DataSource dataSource; - public TransactionExecutor(TransactionManager transactionManager) { - this.transactionManager = transactionManager; + public TransactionExecutor(DataSource dataSource) { + this.dataSource = dataSource; } public void execute(final Runnable runnable) { + TransactionManager transactionManager = new TransactionManager(dataSource); try { transactionManager.begin(); runnable.run(); @@ -24,6 +26,7 @@ public void execute(final Runnable runnable) { } public R executeWithResult(final Supplier supplier) { + TransactionManager transactionManager = new TransactionManager(dataSource); try { transactionManager.begin(); R result = supplier.get(); diff --git a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java index 6551bb2d0f..50940fbcf2 100644 --- a/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/TransactionManager.java @@ -10,7 +10,7 @@ public class TransactionManager { private final DataSource dataSource; - private Connection connection; + private final Connection connection; public TransactionManager(DataSource dataSource) { this.dataSource = dataSource; @@ -19,9 +19,6 @@ public TransactionManager(DataSource dataSource) { public void begin() { try { - if (connection.isClosed()) { - connection = DataSourceUtils.getConnection(dataSource); - } connection.setAutoCommit(false); } catch (SQLException e) { throw new DataAccessException(e);