-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 4단계] 히이로(문제웅) 미션 제출합니다. (#569)
* feat: UserService의 changePassword 로직에 트랜잭션 적용 * refactor: SQLExceptionTranslator static화 * feat: DataSourceUtils 클래스를 통한 커넥션 동기화 기능 구현 * feat: AppService와 TxService 분리 * chore: 윈도우용 테스트 설정 변경 * test: 트랜잭션 학습 테스트 진행 * refactor: JdbcTemplate 가변인자 nullable 어노테이션 추가 선언 * refactor: callback 메서드 checked Exception에서 DataAccessException 제거 * refactor: JdbcTemplate update메서드도 executeQuery를 사용하도록 리팩토링 * refactor: JdbcTemplate 메서드 인자 final 선언 * refactor: TxUserService에 템플릿 콜백 패턴 적용
- Loading branch information
1 parent
400df75
commit abd233c
Showing
19 changed files
with
222 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/techcourse/service/AppUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
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)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/techcourse/service/TransactionCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.techcourse.service; | ||
|
||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface TransactionCallback<T> { | ||
|
||
T doInTransaction() throws SQLException; | ||
} |
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/techcourse/service/TxUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
import org.springframework.jdbc.support.SQLExceptionTranslator; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final DataSource dataSource; | ||
private final UserService userService; | ||
|
||
public TxUserService(UserService userService) { | ||
this.dataSource = DataSourceConfig.getInstance(); | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
TransactionCallback<User> action = () -> userService.findById(id); | ||
return doTransactionWithReturn(action); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
TransactionCallback<Void> action = () -> { | ||
userService.insert(user); | ||
return null; | ||
}; | ||
doTransactionWithoutReturn(action); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
TransactionCallback<Void> action = () -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
}; | ||
doTransactionWithoutReturn(action); | ||
} | ||
|
||
private <T> T doTransactionWithReturn(TransactionCallback<T> action) { | ||
Connection conn = DataSourceUtils.getConnection(dataSource); | ||
T result; | ||
try { | ||
try { | ||
conn.setAutoCommit(false); | ||
result = action.doInTransaction(); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw e; | ||
} | ||
} catch (SQLException e) { | ||
throw SQLExceptionTranslator.translate("", e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
return result; | ||
} | ||
|
||
private <T> void doTransactionWithoutReturn(TransactionCallback<T> action) { | ||
Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
try { | ||
conn.setAutoCommit(false); | ||
action.doInTransaction(); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw e; | ||
} | ||
} catch (SQLException e) { | ||
throw SQLExceptionTranslator.translate("", e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,11 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.springframework.jdbc.support.SQLExceptionTranslator; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.dataSource = DataSourceConfig.getInstance(); | ||
} | ||
|
||
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); | ||
|
||
try (final Connection conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw e; | ||
} | ||
} catch (SQLException e) { | ||
throw SQLExceptionTranslator.translate("", e); | ||
} | ||
} | ||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.