-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: TransactionManager에서 전달받을 함수형 인터페이스를 명시적으로 정의
- Loading branch information
Showing
5 changed files
with
73 additions
and
42 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
55 changes: 55 additions & 0 deletions
55
jdbc/src/main/java/org/springframework/transaction/support/JdbcTransactionManager.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,55 @@ | ||
package org.springframework.transaction.support; | ||
|
||
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 JdbcTransactionManager implements TransactionManager { | ||
|
||
@Override | ||
public void execute(DataSource dataSource, TransactionExecutor method) { | ||
final var connection = TransactionManager.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
method.execute(); | ||
connection.commit(); | ||
} catch (RuntimeException | SQLException e) { | ||
handleTransactionException(connection, e); | ||
} finally { | ||
cleanUpTransaction(dataSource, connection); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T executeAndReturn(DataSource dataSource, TransactionSupplier<T> method) { | ||
final var connection = TransactionManager.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
T result = method.get(); | ||
connection.commit(); | ||
return result; | ||
} catch (RuntimeException | SQLException e) { | ||
handleTransactionException(connection, e); | ||
return null; | ||
} finally { | ||
cleanUpTransaction(dataSource, connection); | ||
} | ||
} | ||
|
||
private static void handleTransactionException(Connection connection, Exception e) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} catch (SQLException rollbackException) { | ||
throw new RuntimeException(rollbackException); | ||
} | ||
} | ||
|
||
private void cleanUpTransaction(DataSource dataSource, Connection connection) { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} |
32 changes: 3 additions & 29 deletions
32
jdbc/src/main/java/org/springframework/transaction/support/TransactionExecutor.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,32 +1,6 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
import java.util.function.Supplier; | ||
|
||
public class TransactionExecutor implements TransactionManager { | ||
|
||
@Override | ||
public <T> T execute(DataSource dataSource, Supplier<T> method) { | ||
final var connection = TransactionManager.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
T result = method.get(); | ||
connection.commit(); | ||
return result; | ||
} catch (RuntimeException | SQLException e) { | ||
try { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} catch (SQLException rollbackException) { | ||
throw new RuntimeException(rollbackException); | ||
} | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
@FunctionalInterface | ||
public interface TransactionExecutor { | ||
void execute(); | ||
} |
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
6 changes: 6 additions & 0 deletions
6
jdbc/src/main/java/org/springframework/transaction/support/TransactionSupplier.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,6 @@ | ||
package org.springframework.transaction.support; | ||
|
||
@FunctionalInterface | ||
public interface TransactionSupplier<T> { | ||
T get(); | ||
} |