Skip to content

Commit

Permalink
refactor: Executor 클래스 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchannn committed Oct 10, 2023
1 parent e8eba25 commit 81839f9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 77 deletions.
19 changes: 2 additions & 17 deletions app/src/main/java/com/techcourse/service/AppUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
61 changes: 6 additions & 55 deletions app/src/main/java/com/techcourse/service/TxUserService.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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));
}
}
8 changes: 3 additions & 5 deletions app/src/main/java/com/techcourse/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.jdbc.core.transaction;

@FunctionalInterface
public interface Executor {

Object execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.springframework.jdbc.core.transaction;

import java.sql.SQLException;

@FunctionalInterface
public interface ExecutorWithoutReturnValue {

void execute() throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 81839f9

Please sign in to comment.