From f21c08ec6f9bea5fc2fa8bacd374f412eb288a0d Mon Sep 17 00:00:00 2001 From: swonny Date: Sat, 14 Oct 2023 18:58:09 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=8A=B8=EB=9E=9C=EC=9E=AD?= =?UTF-8?q?=EC=85=98=20=EB=A7=A4=EB=8B=88=EC=97=90=EC=84=9C=20=EC=BB=A4?= =?UTF-8?q?=EB=B0=8B,=20=EB=A1=A4=EB=B0=B1=20=EC=88=98=ED=96=89=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/TransactionManager.java | 39 ++++++++++++++- .../TransactionSynchronizationManager.java | 48 ++----------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/techcourse/service/TransactionManager.java b/app/src/main/java/com/techcourse/service/TransactionManager.java index 9e451a6498..af826a0455 100644 --- a/app/src/main/java/com/techcourse/service/TransactionManager.java +++ b/app/src/main/java/com/techcourse/service/TransactionManager.java @@ -1,8 +1,12 @@ package com.techcourse.service; +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; +import java.sql.SQLException; public class TransactionManager { @@ -13,12 +17,45 @@ public TransactionManager(final DataSource dataSource) { } public T execute(final TransactionCallback callback) { - TransactionSynchronizationManager.startNewTransaction(dataSource); + final Connection connection = TransactionSynchronizationManager.startNewTransaction(dataSource); final T result = callback.execute(); + commitTransaction(connection); TransactionSynchronizationManager.finishTransaction(dataSource); return result; } + + public static void commitTransaction(final Connection connection) { + try { + connection.commit(); + + clear(connection); + } catch (final SQLException ex) { + rollback(connection); + + throw new DataAccessException("실행 중 예외가 발생했습니다."); + } + } + + public static void rollback(final Connection connection) { + try { + connection.rollback(); + + clear(connection); + } catch (final SQLException ex) { + throw new DataAccessException("트랜잭션 롤백 중 예외가 발생했습니다."); + } + } + + private static void clear(final Connection connection) { + try { + connection.setAutoCommit(true); + connection.close(); + DataSourceUtils.releaseConnection(connection); + } catch (final SQLException ex) { + throw new DataAccessException("커넥션 종료 중 예외가 발생했습니다."); + } + } } 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 f2e2db98e2..8758f95954 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -1,6 +1,5 @@ package org.springframework.transaction.support; -import org.springframework.dao.DataAccessException; import org.springframework.jdbc.datasource.DataSourceUtils; import javax.sql.DataSource; @@ -24,6 +23,11 @@ public static Connection getResource(final DataSource dataSource) throws SQLExce return resources.get().get(dataSource); } + public static Connection startNewTransaction(final DataSource dataSource) { + isActive.set(true); + return bindResource(dataSource); + } + private static Connection bindResource(final DataSource dataSource) { try { final Connection connection = DataSourceUtils.getConnection(dataSource); @@ -35,51 +39,9 @@ private static Connection bindResource(final DataSource dataSource) { } } - public static void startNewTransaction(final DataSource dataSource) { - // TODO: 2023/10/14 transaction이 active인 경우 기존 트랜잭션 참여 여부에 따라 새로 생성하거나 기존 트랜잭션에 참여하도록 하는 것도 필요할까? - isActive.set(true); - bindResource(dataSource); - } - - public static void commitTransaction(final DataSource dataSource) { - try { - final Connection connection = resources.get().get(dataSource); - connection.commit(); - } catch (final SQLException ex) { - rollback(dataSource); - - throw new DataAccessException("실행 중 예외가 발생했습니다."); - } - } - - public static void rollback(final DataSource dataSource) { - try { - if (!isActive.get()) { - final Connection connection = resources.get().get(dataSource); - connection.rollback(); - - clear(dataSource); - } - } catch (final SQLException ex) { - throw new DataAccessException("트랜잭션 롤백 중 예외가 발생했습니다."); - } - } - - private static void clear(final DataSource dataSource) { - try { - final Connection connection = resources.get().get(dataSource); - connection.setAutoCommit(true); - connection.close(); - DataSourceUtils.releaseConnection(connection); - } catch (final SQLException ex) { - throw new DataAccessException("실행 중 예외가 발생했습니다."); - } - } - public static void finishTransaction(final DataSource dataSource) { isActive.set(false); unbindResource(dataSource); - clear(dataSource); } public static Connection unbindResource(final DataSource dataSource) {