-
Notifications
You must be signed in to change notification settings - Fork 300
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단계] 레오(차영호) 미션 제출합니다. #591
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 레오!
레벨4가 진짜 마무리되어가는게 체감되네요 🫨
구현 꼼꼼하게 잘 해주셔서 코멘트가 많이는 없어요 !!
몇가지 제안드리고 싶은 부분들이 있어서 RC 드리겠습니다~!
다음 미션 하시느라 바쁘시겠지만,,,, 화이팅!!!
public <T> T transaction(TransactionExecutor<T> transactionExecutor) { | ||
Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
transactionExecutor.execute(conn); | ||
conn.setAutoCommit(false); | ||
T result = transactionExecutor.execute(); | ||
conn.commit(); | ||
conn.setAutoCommit(true); | ||
return result; | ||
} catch (SQLException | DataAccessException e) { | ||
e.printStackTrace(); | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분 저도 고민하던 부분이에요!
레오 말씀처럼 반환값 없는 경우엔 null을 반환해줘야하니..
방금 떠오른건데 반환값이 있는 TransactionExecutor와 반환값이 없는 VoidTransactionExecutor를 따로 정의해 사용하는 방법도 있을 것 같아요!!
너무 자세한 리뷰는 스포가 될 것 같으니 혹시 제 의견이 더 궁금하면 dm이나 답글 남겨주세요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반영해봤습니다!
private void rollback(Connection conn) { | ||
try { | ||
conn.rollback(); | ||
conn.setAutoCommit(true); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} finally { | ||
conn.close(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private 메서드 분리로 이중 try 구문을 없앴군요 👍
if (resource == null) { | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NPE 방지 👍
|
||
public static void bindResource(DataSource key, Connection value) { | ||
final Map<DataSource, Connection> connectionMap = new HashMap<>(Map.of(key, value)); | ||
resources.set(connectionMap); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재는 bindResource()
호출시 매번 HashMap을 생성해주는데, 그러면 기존에 존재하던 connection 정보들이 덮어써질 것 같아요
private static final ThreadLocal<Map<DataSource, Connection>> resources = ThreadLocal.withInitial(HashMap::new);
이런식으로 클래스 생성시 초기화 해주고, bindResource()
호출시 해당 HashMap에 connection을 등록해주는 형식으로 가면 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분도 반영해봤습니다!
반영하는 과정에서 getResources, unbinedResource 의 null 체크 과정이 빠졌는데요. 처음에 무조건 초기화를 하기 때문에 불필요하다고 생각해 제거했습니다!
안녕하세요 애쉬! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기나긴 미션이 끝났네요 👍
수고 많으셨습니다 레오!!
우테코 마지막 미션까지 화이팅하세요!!!!
public <T> T transaction(TransactionExecutor<T> transactionExecutor) { | ||
Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
transactionExecutor.execute(conn); | ||
conn.setAutoCommit(false); | ||
T result = transactionExecutor.execute(); | ||
conn.commit(); | ||
conn.setAutoCommit(true); | ||
return result; | ||
} catch (SQLException | DataAccessException e) { | ||
conn.rollback(); | ||
e.printStackTrace(); | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
얘도 TransactionExecutor대신 그냥 Supplier로 받으면 되겠군요 👍
안녕하세요 애쉬. 어느덧 마지막 단계네요..
미션 제출 늦어져서 죄송합니다..
현재 TxUserService 에서
insert()
,changePassword()
는 null 을 반환하고 있는데, 이 부분은 좀 마음에 안드네요.. 이를 고치고 싶었지만 제출이 너무 늦어질 것 같아 방치한채로 제출합니다..이번주도 화이팅입니다 ㅎㅎ😎