-
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단계] 가비 미션 제출합니다. (#523)
* refactor: 메서드명 수정 * fix: DataSourceUtil과 TransactionSynchronizationManager를 이용하여 Connection을 사용하도록 변경 * refactor: findById의 반환 타입에서 Optional 제거 * feat: DataSourceUtils 내 TransactionSynchronizationManger 자원 해제 추가 * feat: TransactionManager 구현 * feat: UserService 추상화 * feat: AppUserService, TxUserService 구현 * test: testTransactionRollback() 메서드를 LMS에 있는 내용으로 변경 * fix : jdbctemplate에서 connection 해제하는 부분 제거 * refactor: Connection 선언부를 try-catch 밖으로 분리 * refactor: TranactionManager의 메서드 반환값을 void로 변경 * chore: final 키워드 통일 * fix: TransactionSynchronizationManager.unbindResource(dataSource) 중복 호출 제거 * fix: 불필요한 static 제거 * feat: DatabasePopulatorUtils도 DataSoureUtils를 이용해 connection을 가지고 오도록 변경 * refactor: service에서 TransactionManager를 주입받도록 변경 * refactor: 템플릿 콜백 패턴 적용 * refactor: 함수형 인터페이스 실행 메서드 분리 * refactor: DatasourcePopulatorUtils connection 해제 추가
- Loading branch information
Showing
14 changed files
with
180 additions
and
118 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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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).orElseThrow(); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final User user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionManager transactionManager; | ||
|
||
public TxUserService(final UserService userService, final TransactionManager transactionManager) { | ||
this.userService = userService; | ||
this.transactionManager = transactionManager; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionManager.runForObject(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionManager.run(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.run(() -> userService.changePassword(id, newPassword, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,13 @@ | ||
package com.techcourse.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
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 org.springframework.dao.DataAccessException; | ||
|
||
public class UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public Optional<User> findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
public interface UserService { | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
Connection connection = null; | ||
try { | ||
connection = DataSourceConfig.getInstance().getConnection(); | ||
connection.setAutoCommit(false); | ||
User findById(final long id); | ||
|
||
final User user = findById(id).orElseThrow(); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
void insert(final User user); | ||
|
||
connection.commit(); | ||
} catch (final SQLException e) { | ||
try { | ||
connection.rollback(); | ||
} catch (final SQLException rollbackException) { | ||
throw new DataAccessException(rollbackException); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
try { | ||
connection.close(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} | ||
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
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
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.