Skip to content

Commit

Permalink
fix: Connection 반환되지 않는 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
dooboocookie committed Oct 10, 2023
1 parent e9557b6 commit 6e63c56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ConnectionTemplate(final DataSource dataSource) {
public <T> T readResult(final String sql,
final SelectQueryExecutor<T> selectQueryExecutor,
final Object... parameters) {
final Connection connection = DataSourceUtils.getConnection(dataSource);
final Connection connection = DataSourceUtils.getConnectionForTransactionSupports(dataSource);
try (final PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
setQueryParameter(preparedStatement, parameters);
final ResultSet resultSet = preparedStatement.executeQuery();
Expand All @@ -73,14 +73,14 @@ public <T> T readResult(final String sql,
log.error(e.getMessage(), e);
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnectionIfNotInTransaction(connection, dataSource);
DataSourceUtils.releaseConnectionForTransactionSupports(connection, dataSource);
}
}

public void update(final String sql,
final UpdateQueryExecutor updateQueryExecutor,
final Object... parameters) {
final Connection connection = DataSourceUtils.getConnection(dataSource);
final Connection connection = DataSourceUtils.getConnectionForTransactionSupports(dataSource);
try (final PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
setQueryParameter(preparedStatement, parameters);

Expand All @@ -89,7 +89,7 @@ public void update(final String sql,
log.error(e.getMessage(), e);
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnectionIfNotInTransaction(connection, dataSource);
DataSourceUtils.releaseConnectionForTransactionSupports(connection, dataSource);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class DataSourceUtils {

private DataSourceUtils() {}

public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
public static Connection getConnectionForTransactionRequired(DataSource dataSource) throws CannotGetJdbcConnectionException {
Connection connection = TransactionSynchronizationManager.getResource(dataSource);
if (connection != null) {
return connection;
Expand All @@ -26,7 +26,7 @@ public static Connection getConnection(DataSource dataSource) throws CannotGetJd
}
}

public static void releaseConnection(Connection connection, DataSource dataSource) {
public static void releaseConnectionForTransactionRequired(Connection connection, DataSource dataSource) {
try {
connection.close();
TransactionSynchronizationManager.unbindResource(dataSource);
Expand All @@ -35,7 +35,20 @@ public static void releaseConnection(Connection connection, DataSource dataSourc
}
}

public static void releaseConnectionIfNotInTransaction(Connection connection, DataSource dataSource) {
public static Connection getConnectionForTransactionSupports(DataSource dataSource) throws CannotGetJdbcConnectionException {
Connection connection = TransactionSynchronizationManager.getResource(dataSource);
if (connection != null) {
return connection;
}

try {
return dataSource.getConnection();
} catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex);
}
}

public static void releaseConnectionForTransactionSupports(Connection connection, DataSource dataSource) {
if (TransactionSynchronizationManager.isActiveTransaction(dataSource)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.slf4j.LoggerFactory;
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;
Expand All @@ -21,7 +20,7 @@ public Transaction(final DataSource dataSource) {
}

public <T> T run(final TransactionTemplate<T> transactionTemplate) {
final Connection connection = DataSourceUtils.getConnection(dataSource);
final Connection connection = DataSourceUtils.getConnectionForTransactionRequired(dataSource);
start(connection);
try {
final T result = transactionTemplate.execute(connection);
Expand All @@ -32,7 +31,7 @@ public <T> T run(final TransactionTemplate<T> transactionTemplate) {
rollback(connection);
throw new DataAccessException(exception);
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
DataSourceUtils.releaseConnectionForTransactionRequired(connection, dataSource);
}
}

Expand Down

0 comments on commit 6e63c56

Please sign in to comment.