-
Notifications
You must be signed in to change notification settings - Fork 379
[3단계 - Transaction 적용하기] 메이(김시원) 미션 제출합니다. #1131
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
base: seaniiio
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.interface21.transaction; | ||
|
||
import com.interface21.dao.DataAccessException; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
|
||
public class TransactionManager { | ||
|
||
private final DataSource dataSource; | ||
|
||
public TransactionManager(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public void executeTransaction(final TransactionalAction action) { | ||
try (final var connection = dataSource.getConnection();) { | ||
connection.setAutoCommit(false); | ||
|
||
try { | ||
action.execute(connection); | ||
connection.commit(); | ||
} catch (Exception e) { | ||
connection.rollback(); | ||
throw new DataAccessException("트랜잭션 내부 작업 수행 중 오류가 발생하여 롤백이 수행되었습니다."); | ||
} | ||
Comment on lines
+15
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 메서드의 들여쓰기 깊이를 줄여볼 수 있을까요? 현재 객체지향 생활 체조 규칙 1번은 "한 메서드에 오직 한 단계의 들여쓰기만"을 권장합니다. 이는 메서드의 복잡도를 낮추고 각 메서드가 하나의 책임만 갖도록 하기 위함입니다. 중첩된 트랜잭션 로직을 별도 메서드로 분리하면 어떨까요? 힌트: Connection을 받아서 트랜잭션을 실행하는 부분을 별도 메서드로 추출해보세요. 🤖 Prompt for AI Agents
|
||
|
||
} catch (SQLException e) { | ||
throw new RuntimeException("트랜잭션 로직을 실행하는 중 Connection 오류가 발생했습니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.interface21.transaction; | ||
|
||
import java.sql.Connection; | ||
|
||
@FunctionalInterface | ||
public interface TransactionalAction { | ||
|
||
void execute(Connection 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.
🛠️ Refactor suggestion | 🟠 Major
의존성 주입 패턴에 대해 다시 생각해볼까요?
현재
UserService
는TransactionManager
를 직접 생성하고 있습니다(19줄). 하지만UserDao
와UserHistoryDao
는 생성자를 통해 주입받고 있습니다.이런 차이가 발생한 이유는 무엇일까요? 만약
TransactionManager
도 외부에서 주입받는다면 어떤 장점이 있을까요?힌트: 테스트 작성 시나 다른 트랜잭션 구현체로 교체해야 할 때를 생각해보세요.
🤖 Prompt for AI Agents