diff --git a/app/src/main/java/com/techcourse/service/AppUserService.java b/app/src/main/java/com/techcourse/service/AppUserService.java index a0f057836c..b01d3c6206 100644 --- a/app/src/main/java/com/techcourse/service/AppUserService.java +++ b/app/src/main/java/com/techcourse/service/AppUserService.java @@ -4,12 +4,8 @@ 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.DataSourceUtils; import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; import java.util.NoSuchElementException; public class AppUserService implements UserService { @@ -35,19 +31,8 @@ public User findById(final long id) { } @Override - public void insert(final User user) throws SQLException { - final Connection conn = DataSourceUtils.getConnection(dataSource); - conn.setAutoCommit(false); - try { - userDao.insert(user); - - conn.commit(); - } catch (SQLException e) { - conn.rollback(); - throw new DataAccessException(e); - } finally { - DataSourceUtils.releaseConnection(conn, dataSource); - } + public void insert(final User user) { + userDao.insert(user); } @Override diff --git a/app/src/main/java/com/techcourse/service/TxUserService.java b/app/src/main/java/com/techcourse/service/TxUserService.java index b2eb2952ea..100ddb942a 100644 --- a/app/src/main/java/com/techcourse/service/TxUserService.java +++ b/app/src/main/java/com/techcourse/service/TxUserService.java @@ -1,13 +1,9 @@ package com.techcourse.service; import com.techcourse.domain.User; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.jdbc.core.transaction.TxExecutor; import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; public class TxUserService implements UserService { @@ -23,61 +19,16 @@ public TxUserService( @Override public User findById(final long id) { - final Connection conn = DataSourceUtils.getConnection(dataSource); - try { - conn.setAutoCommit(false); - final User user = userService.findById(id); - - conn.commit(); - return user; - } catch (SQLException e) { - rollback(conn); - throw new DataAccessException(e); - } finally { - DataSourceUtils.releaseConnection(conn, dataSource); - TransactionSynchronizationManager.unbindResource(dataSource); - } - } - - private void rollback(final Connection conn) { - try { - conn.rollback(); - } catch (SQLException e) { - throw new DataAccessException(e); - } + return (User) TxExecutor.execute(dataSource, () -> userService.findById(id)); } @Override - public void insert(final User user) throws SQLException { - final Connection conn = DataSourceUtils.getConnection(dataSource); - conn.setAutoCommit(false); - try { - userService.insert(user); - - conn.commit(); - } catch (SQLException e) { - rollback(conn); - throw new DataAccessException(e); - } finally { - DataSourceUtils.releaseConnection(conn, dataSource); - TransactionSynchronizationManager.unbindResource(dataSource); - } + public void insert(final User user) { + TxExecutor.executeWithoutReturnValue(dataSource, () -> userService.insert(user)); } @Override - public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException { - final Connection conn = DataSourceUtils.getConnection(dataSource); - conn.setAutoCommit(false); - try { - userService.changePassword(id, newPassword, createBy); - - conn.commit(); - } catch (SQLException e) { - rollback(conn); - throw new DataAccessException(e); - } finally { - DataSourceUtils.releaseConnection(conn, dataSource); - TransactionSynchronizationManager.unbindResource(dataSource); - } + public void changePassword(final long id, final String newPassword, final String createBy) { + TxExecutor.executeWithoutReturnValue(dataSource, () -> 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 7f2430e9ed..b14dbcacbf 100644 --- a/app/src/main/java/com/techcourse/service/UserService.java +++ b/app/src/main/java/com/techcourse/service/UserService.java @@ -2,13 +2,11 @@ import com.techcourse.domain.User; -import java.sql.SQLException; - public interface UserService { - User findById(final long id) throws SQLException; + User findById(final long id); - void insert(final User user) throws SQLException; + void insert(final User user); - void changePassword(final long id, final String newPassword, final String createBy) throws SQLException; + void changePassword(final long id, final String newPassword, final String createBy); } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/transaction/Executor.java b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/Executor.java new file mode 100644 index 0000000000..dbf1c81c5f --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/Executor.java @@ -0,0 +1,7 @@ +package org.springframework.jdbc.core.transaction; + +@FunctionalInterface +public interface Executor { + + Object execute(); +} diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/transaction/ExecutorWithoutReturnValue.java b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/ExecutorWithoutReturnValue.java new file mode 100644 index 0000000000..4ca837ae0a --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/ExecutorWithoutReturnValue.java @@ -0,0 +1,9 @@ +package org.springframework.jdbc.core.transaction; + +import java.sql.SQLException; + +@FunctionalInterface +public interface ExecutorWithoutReturnValue { + + void execute() throws SQLException; +} diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/transaction/TxExecutor.java b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/TxExecutor.java new file mode 100644 index 0000000000..afb2215015 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/transaction/TxExecutor.java @@ -0,0 +1,67 @@ +package org.springframework.jdbc.core.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; +import java.sql.SQLException; + +public class TxExecutor { + + public static void executeWithoutReturnValue( + final DataSource dataSource, + final ExecutorWithoutReturnValue executor + ) { + final Connection conn = DataSourceUtils.getConnection(dataSource); + try { + conn.setAutoCommit(false); + executor.execute(); + + commit(conn); + } catch (SQLException e) { + rollback(conn); + throw new DataAccessException(e); + } finally { + DataSourceUtils.releaseConnection(conn, dataSource); + TransactionSynchronizationManager.unbindResource(dataSource); + } + } + + public static Object execute( + final DataSource dataSource, + final Executor executor + ) { + final Connection conn = DataSourceUtils.getConnection(dataSource); + try { + conn.setAutoCommit(false); + final Object result = executor.execute(); + + conn.commit(); + return result; + } catch (SQLException e) { + rollback(conn); + throw new DataAccessException(e); + } finally { + DataSourceUtils.releaseConnection(conn, dataSource); + TransactionSynchronizationManager.unbindResource(dataSource); + } + } + + private static void commit(final Connection conn) { + try { + conn.commit(); + } catch (SQLException e) { + throw new DataAccessException(e); + } + } + + private static void rollback(final Connection conn) { + try { + conn.rollback(); + } catch (SQLException e) { + throw new DataAccessException(e); + } + } +}