Skip to content

Commit d0f339d

Browse files
committed
refactor: TransactionManager에서 전달받을 함수형 인터페이스를 명시적으로 정의
1 parent e531b69 commit d0f339d

File tree

5 files changed

+73
-42
lines changed

5 files changed

+73
-42
lines changed
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.techcourse.service;
22

33
import com.techcourse.domain.User;
4-
import org.springframework.transaction.support.TransactionExecutor;
4+
import org.springframework.transaction.support.JdbcTransactionManager;
55
import org.springframework.transaction.support.TransactionManager;
66

77
import javax.sql.DataSource;
88

99
public class TxUserService implements UserService {
1010

11-
private final TransactionManager transactionManager = new TransactionExecutor();
11+
private final TransactionManager transactionManager = new JdbcTransactionManager();
1212
private final DataSource dataSource;
1313
private final UserService userService;
1414

@@ -19,22 +19,17 @@ public TxUserService(DataSource dataSource, UserService userService) {
1919

2020
@Override
2121
public User findById(long id) {
22-
return transactionManager.execute(dataSource, () -> userService.findById(id));
22+
return transactionManager.executeAndReturn(dataSource, () -> userService.findById(id));
2323
}
2424

2525
@Override
2626
public void insert(User user) {
27-
transactionManager.execute(dataSource, () -> {
28-
userService.insert(user);
29-
return null;
30-
});
27+
transactionManager.execute(dataSource, () -> userService.insert(user));
3128
}
3229

3330
@Override
3431
public void changePassword(long id, String newPassword, String createBy) {
35-
transactionManager.execute(dataSource, () -> {
36-
userService.changePassword(id, newPassword, createBy);
37-
return null;
38-
});
32+
transactionManager.execute(dataSource, () -> userService.changePassword(id, newPassword, createBy)
33+
);
3934
}
4035
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.springframework.transaction.support;
2+
3+
import org.springframework.dao.DataAccessException;
4+
import org.springframework.jdbc.datasource.DataSourceUtils;
5+
6+
import javax.sql.DataSource;
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
10+
public class JdbcTransactionManager implements TransactionManager {
11+
12+
@Override
13+
public void execute(DataSource dataSource, TransactionExecutor method) {
14+
final var connection = TransactionManager.getConnection(dataSource);
15+
try {
16+
connection.setAutoCommit(false);
17+
method.execute();
18+
connection.commit();
19+
} catch (RuntimeException | SQLException e) {
20+
handleTransactionException(connection, e);
21+
} finally {
22+
cleanUpTransaction(dataSource, connection);
23+
}
24+
}
25+
26+
@Override
27+
public <T> T executeAndReturn(DataSource dataSource, TransactionSupplier<T> method) {
28+
final var connection = TransactionManager.getConnection(dataSource);
29+
try {
30+
connection.setAutoCommit(false);
31+
T result = method.get();
32+
connection.commit();
33+
return result;
34+
} catch (RuntimeException | SQLException e) {
35+
handleTransactionException(connection, e);
36+
return null;
37+
} finally {
38+
cleanUpTransaction(dataSource, connection);
39+
}
40+
}
41+
42+
private static void handleTransactionException(Connection connection, Exception e) {
43+
try {
44+
connection.rollback();
45+
throw new DataAccessException(e);
46+
} catch (SQLException rollbackException) {
47+
throw new RuntimeException(rollbackException);
48+
}
49+
}
50+
51+
private void cleanUpTransaction(DataSource dataSource, Connection connection) {
52+
DataSourceUtils.releaseConnection(connection, dataSource);
53+
TransactionSynchronizationManager.unbindResource(dataSource);
54+
}
55+
}
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
11
package org.springframework.transaction.support;
22

3-
import org.springframework.dao.DataAccessException;
4-
import org.springframework.jdbc.datasource.DataSourceUtils;
5-
6-
import javax.sql.DataSource;
7-
import java.sql.SQLException;
8-
import java.util.function.Supplier;
9-
10-
public class TransactionExecutor implements TransactionManager {
11-
12-
@Override
13-
public <T> T execute(DataSource dataSource, Supplier<T> method) {
14-
final var connection = TransactionManager.getConnection(dataSource);
15-
try {
16-
connection.setAutoCommit(false);
17-
T result = method.get();
18-
connection.commit();
19-
return result;
20-
} catch (RuntimeException | SQLException e) {
21-
try {
22-
connection.rollback();
23-
throw new DataAccessException(e);
24-
} catch (SQLException rollbackException) {
25-
throw new RuntimeException(rollbackException);
26-
}
27-
} finally {
28-
DataSourceUtils.releaseConnection(connection, dataSource);
29-
TransactionSynchronizationManager.unbindResource(dataSource);
30-
}
31-
}
3+
@FunctionalInterface
4+
public interface TransactionExecutor {
5+
void execute();
326
}

jdbc/src/main/java/org/springframework/transaction/support/TransactionManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import javax.sql.DataSource;
66
import java.sql.Connection;
7-
import java.util.function.Supplier;
87

98
public interface TransactionManager {
109

1110
static Connection getConnection(DataSource dataSource) {
1211
return DataSourceUtils.getConnection(dataSource);
1312
}
1413

15-
<T> T execute(DataSource dataSource, Supplier<T> method);
14+
void execute(DataSource dataSource, TransactionExecutor method);
15+
16+
<T> T executeAndReturn(DataSource dataSource, TransactionSupplier<T> method);
1617
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.transaction.support;
2+
3+
@FunctionalInterface
4+
public interface TransactionSupplier<T> {
5+
T get();
6+
}

0 commit comments

Comments
 (0)