Skip to content

Commit

Permalink
feat: 트랜잭션이 활성화 여부에 따라 Connection을 close 하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hong-sile committed Oct 10, 2023
1 parent 6fb201c commit 62292f7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.jdbc.core.exception.DataNotFoundException;
import org.springframework.jdbc.core.exception.JdbcTemplateException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class JdbcTemplate {

Expand Down Expand Up @@ -70,6 +71,11 @@ private <T> T executeSql(
return preparedStatementExecutor.execute(pst);
} catch (final SQLException e) {
throw new JdbcTemplateException(e);
} finally {
if (!TransactionSynchronizationManager.isTransactionActive(dataSource)) {
DataSourceUtils.releaseConnection(connection, dataSource);
TransactionSynchronizationManager.unbindResource(dataSource);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.exception.DataNotFoundException;
import org.springframework.jdbc.core.test_supporter.DataSourceConfig;
import org.springframework.jdbc.core.test_supporter.DatabasePopulatorUtils;
import org.springframework.jdbc.core.test_supporter.User;
import org.springframework.jdbc.core.test_supporter.UserDao;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;

class JdbcTemplateTest {

Expand Down Expand Up @@ -92,4 +103,50 @@ void noDataFoundException() {
assertThatThrownBy(testTarget)
.isInstanceOf(DataNotFoundException.class);
}

@Nested
@DisplayName("트랜잭션이 활성화 여부에 따라 Connection을 close한다.")
class CloseNonTransactionConnection {

private JdbcTemplate jdbcTemplate;
private DataSource dataSource;
private Connection connection;

@BeforeEach
void setUp() throws SQLException {
dataSource = spy(DataSourceConfig.getInstance());
connection = spy(dataSource.getConnection());
when(dataSource.getConnection()).thenReturn(connection);
jdbcTemplate = new JdbcTemplate(dataSource);
}

@Test
@DisplayName("트랜잭션이 활성화되어 있지 않으면 close한다.")
void closeNonTransactionConnection() throws SQLException {
final String sql = "insert into users (account, password, email) values (?, ?, ?)";

jdbcTemplate.executeUpdate(
sql,
USER_FIXTURE.getAccount(), USER_FIXTURE.getPassword(), USER_FIXTURE.getEmail()
);
verify(connection, times(1)).close();
}

@Test
@DisplayName("트랜잭션이 활성화되어 있으면 close하지 않는다.")
void closeTransactionConnection() throws SQLException {
final String sql = "insert into users (account, password, email) values (?, ?, ?)";
final Connection connection = DataSourceUtils.getConnection(dataSource);
connection.setAutoCommit(false);

jdbcTemplate.executeUpdate(
sql,
USER_FIXTURE.getAccount(), USER_FIXTURE.getPassword(), USER_FIXTURE.getEmail()
);

verify(connection, never()).close();
TransactionSynchronizationManager.clear();
connection.close();
}
}
}

0 comments on commit 62292f7

Please sign in to comment.