Skip to content

Commit

Permalink
refactor: 트랜잭션 매니에서 커밋, 롤백 수행하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
swonny committed Oct 14, 2023
1 parent 05a153f commit f21c08e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
39 changes: 38 additions & 1 deletion app/src/main/java/com/techcourse/service/TransactionManager.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -13,12 +17,45 @@ public TransactionManager(final DataSource dataSource) {
}

public <T> T execute(final TransactionCallback<T> 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("커넥션 종료 중 예외가 발생했습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.springframework.transaction.support;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit f21c08e

Please sign in to comment.