Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JDBC 라이브러리 구현하기 - 4단계] 이레(이다형) 미션 제출합니다. #553

Merged
merged 19 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f82dc21
feat: TransactionSynchronizationManager 리소스 bind, unbind, get 기능 추가
zillionme Oct 8, 2023
be773f8
feat: 커넥션 해제 시, 쓰레드 로컬에 저장된 커넥션 제거
zillionme Oct 8, 2023
db74c8e
refactor: 쓰레드 로컬을 통한 커넥션 동기화 적용
zillionme Oct 8, 2023
d6af471
refactor: UserService 인터페이스 생성
zillionme Oct 8, 2023
a308b22
feat: 결과 있는 트랜잭션 실행 기능 추가
zillionme Oct 8, 2023
cbeda5a
feat: AppUserService의 트랜잭션 프록시 클래스 추가
zillionme Oct 8, 2023
df0bddc
test: 테스트 수정
zillionme Oct 8, 2023
b8bcbe5
chore: 예외 클래스 패키지 이동
zillionme Oct 8, 2023
3b7869f
test: aop 테스트
zillionme Oct 9, 2023
9470118
chore: final 키워드 수정 및 필요없는 메서드 지우기
zillionme Oct 9, 2023
a7b16bb
chore: 필요없는 클래스 삭제
zillionme Oct 10, 2023
046b3c8
refactor: 커넥션의 트랜잭션 active 상태에 따른 커넥션 해제 방법 수정
zillionme Oct 10, 2023
8cf6bda
refactor: TransactionManager 트랜잭션 시작과 종료 관리 기능 추가
zillionme Oct 10, 2023
2bfef4b
refactor: JdbcTemplate 커넥션 해제 기능 추가
zillionme Oct 10, 2023
8cf2002
fix: 트랜잭션 동기화 매니저 unbind에 스레드 로컬에 대한 자원 해제 기능 추가
zillionme Oct 11, 2023
237a3a3
refactor: 트랜잭션 매니저에서 발생하는 SQL예외 처리방법 수정
zillionme Oct 11, 2023
6d05798
refactor: 중복 코드 제거
zillionme Oct 11, 2023
b2a6103
chore: 주석 제거
zillionme Oct 11, 2023
22da3a4
refaco: 트랜잭션 동기화 매니저 unbind에 스레드 로컬에 대한 자원 해제 기능 추가
zillionme Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.springframework.jdbc.datasource;

import org.springframework.jdbc.exception.CannotGetJdbcConnectionException;
import org.springframework.transaction.support.ConnectionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.sql.DataSource;
Expand All @@ -13,24 +14,32 @@ public abstract class DataSourceUtils {
private DataSourceUtils() {}

public static Connection getConnection(final DataSource dataSource) throws CannotGetJdbcConnectionException {
Connection connection = TransactionSynchronizationManager.getResource(dataSource);
if (connection != null) {
return connection;
ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource);
if (connectionHolder != null) {
return connectionHolder.getConnection();
}

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

public static void releaseConnection(final Connection connection, final DataSource dataSource) {
if(connection == null) {
return;
}
ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource);
boolean isConnectionActive = connectionHolder.isConnectionTransactionActive();
if(isConnectionActive) {
return;
}
try {
connection.close();
TransactionSynchronizationManager.unbindResource(dataSource);
connection.close();
} catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.springframework.transaction.support;

import java.sql.Connection;

public class ConnectionHolder {

private final Connection connection;

private boolean isConnectionTransactionActive;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자에서도 따로 값을 받지 않고 있는데, default값을 설정해주는 것이 어떨까요?
지금과 같은 경우 setter를 사용하지 않으면 값이 할당되지 않을 것 같아요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolean은 기본형이기 때문에 false가 기본값으로 설정되는 것으로 알고 있습니다!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그러네요.. 죄송합니다..ㅎㅎ

제안드리고 싶었던 점은 기본 값을 false로 명시해두고,
setter(false)를 이용하지 않는 방법이 더 좋아보여서 말씀드렸습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그렇게 수정하겠숩니다!


public ConnectionHolder(final Connection connection) {
this.connection = connection;
}

public void setConnectionTransactionActive(boolean isActive) {
this.isConnectionTransactionActive = isActive;
}

public Connection getConnection() {
return connection;
}

public boolean isConnectionTransactionActive() {
return isConnectionTransactionActive;
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package org.springframework.transaction.support;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

public abstract class TransactionSynchronizationManager {

private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>();
private static final ThreadLocal<Map<DataSource, ConnectionHolder>> resources = new ThreadLocal<>();

private TransactionSynchronizationManager() {
}

public static Connection getResource(DataSource key) {
Map<DataSource, Connection> map = resources.get();
public static ConnectionHolder getResource(DataSource key) {
Map<DataSource, ConnectionHolder> map = resources.get();
if (map == null) {
return null;
}
return map.getOrDefault(key, null);
}

public static void bindResource(DataSource key, Connection value) {
Map<DataSource, Connection> map = resources.get();
public static void bindResource(DataSource key, ConnectionHolder value) {
Map<DataSource, ConnectionHolder> map = resources.get();
if (map == null) {
map = new HashMap<>();
resources.set(map);
}
map.put(key, value);
}

public static Connection unbindResource(DataSource key) {
Map<DataSource, Connection> map = resources.get();
public static ConnectionHolder unbindResource(DataSource key) {
Map<DataSource, ConnectionHolder> map = resources.get();
if (map == null || !map.containsKey(key)) {
return null;
}
Expand Down