Skip to content

Commit

Permalink
refactor: transactionManager의 doInTransaction메서드가 반환값을 갖도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
poi1649 committed Oct 10, 2023
1 parent 19f288c commit 22661c8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/com/techcourse/service/TxUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public void insert(final User user) {

@Override
public void changePassword(final long id, final String newPassword, final String createBy) {
transactionManager.doInTransaction(() -> userService.changePassword(id, newPassword, createBy));
transactionManager.doInTransaction(() -> {
userService.changePassword(id, newPassword, createBy);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;
Expand All @@ -14,20 +16,23 @@ public TransactionManager(final DataSource dataSource) {
this.dataSource = dataSource;
}

public void doInTransaction(final Runnable runnable) throws DataAccessException {
@Nullable
public <T> T doInTransaction(final Supplier<T> supplier) throws DataAccessException {
T result = null;
try (final var connection = DataSourceUtils.getConnection(dataSource)) {
TransactionSynchronizationManager.bindResource(dataSource, connection);
connection.setAutoCommit(false);
runnable.run();
result = supplier.get();
connection.commit();
} catch (final SQLException e) {
} catch (final Exception e) {
doRollback(e, TransactionSynchronizationManager.getResource(dataSource));
} finally {
TransactionSynchronizationManager.unbindResource(dataSource);
}
return result;
}

private void doRollback(final SQLException e, final Connection connection) {
private void doRollback(final Exception e, final Connection connection) {
try {
connection.rollback();
throw new DataAccessException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public abstract class TransactionSynchronizationManager {

private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>();
private static final ThreadLocal<Map<DataSource, Connection>> resources = ThreadLocal.withInitial(HashMap::new);

private TransactionSynchronizationManager() {
}
Expand All @@ -25,13 +25,6 @@ static Connection unbindResource(final DataSource key) {
}

private static Map<DataSource, Connection> getThreadLocalResource() {
if (resources.get() == null) {
initSynchronization();
}
return resources.get();
}

private static void initSynchronization() {
resources.set(new HashMap<>());
}
}

0 comments on commit 22661c8

Please sign in to comment.