-
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단계] 이레(이다형) 미션 제출합니다. #553
Changes from all commits
f82dc21
be773f8
db74c8e
d6af471
a308b22
cbeda5a
df0bddc
b8bcbe5
3b7869f
9470118
a7b16bb
046b3c8
8cf6bda
2bfef4b
8cf2002
237a3a3
6d05798
b2a6103
22da3a4
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,32 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class AppUserService implements UserService{ | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public AppUserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final TransactionManager transactionManager; | ||
private final UserService userService; | ||
|
||
public TxUserService(final TransactionManager transactionManager, final UserService userService) { | ||
this.transactionManager = transactionManager; | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionManager.executeWithResult(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionManager.execute(() -> userService.insert(user)); | ||
} | ||
|
||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.execute(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,12 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.transaction.support.TransactionManager; | ||
|
||
import java.sql.Connection; | ||
public interface UserService { | ||
|
||
public class UserService { | ||
User findById(final long id); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
void insert(final User user); | ||
|
||
private final TransactionManager transactionManager; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao, final TransactionManager transactionManager) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.transactionManager = transactionManager; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.execute((conn) -> changePasswordInTransaction(id, newPassword, createBy, conn)); | ||
} | ||
|
||
private void changePasswordInTransaction(final long id, final String newPassword, final String createBy, final Connection conn) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.springframework.jdbc; | ||
package org.springframework.jdbc.exception; | ||
|
||
import java.sql.SQLException; | ||
|
||
|
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; | ||
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. 생성자에서도 따로 값을 받지 않고 있는데, default값을 설정해주는 것이 어떨까요? 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. boolean은 기본형이기 때문에 false가 기본값으로 설정되는 것으로 알고 있습니다! 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. 엇 그러네요.. 죄송합니다..ㅎㅎ 제안드리고 싶었던 점은 기본 값을 false로 명시해두고, 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. 앗 그렇게 수정하겠숩니다! |
||
|
||
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; | ||
} | ||
} |
This file was deleted.
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.
여기서 커넥션을 close하는 로직을 추가해주셨는데 이유가 있을까요?
제가 생각했을 때 JdbcTemplate에서는 커넥션을 가져오기는 하지만,
close할 때는 TransactionManager클래스의 executeWithResult메서드가 끝나는 시점에 해준다고 이해를 했어요.
따라서 결국 비즈니스 로직이 실행되고 마지막에 단 한번만 호출되면 된다고 생각했습니다.
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.
만약 트랜잭션이 없는 커넥션의 경우에는 (= 서비스에서 트랜잭션 매니저 클래스를 사용하지 않고, 매번 새로운 커넥션을 생성하는 방식으로 쿼리작업이 이뤄지는 경우)
커넥션이 해제되지 않기 때문입니다.
위와 같이 작업할 경우,
JdbcTemplate에서 커넥션을 생성하는 것(트랜잭션이 없는 커넥션 = 커넥션 홀더 내의 커넥션의 트랜잭션활성화 상태가 false)과
TransactionManager에서 커넥션을 생성하는 것(트랜잭션이 있는 커넥션 = 커넥션 홀더 내의 커넥션의 트랜잭션활성화 상태가 true)이 분리됩니다.