-
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단계] 모디(전제희) 미션 제출합니다. #597
Changes from all commits
8170686
ba64ed4
c5891dc
76da712
ce8a635
d279c99
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) { | ||
User 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,111 @@ | ||
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.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final AppUserService appUserService; | ||
private final DataSource datasource = DataSourceConfig.getInstance(); | ||
|
||
public TxUserService(AppUserService appUserService) { | ||
this.appUserService = appUserService; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return execute(() -> appUserService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
execute(() -> appUserService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
execute(() -> appUserService.changePassword(id, newPassword, createBy)); | ||
} | ||
|
||
private void execute(ExecutableWithoutReturn executable) { | ||
try (TransactionExecutor executor = TransactionExecutor.initTransaction(datasource)) { | ||
try { | ||
executable.execute(); | ||
executor.commit(); | ||
} catch (Exception e) { | ||
executor.rollback(); | ||
throw new DataAccessException(); | ||
} | ||
} | ||
} | ||
|
||
private <T> T execute(ExecutableWithReturn<T> executable) { | ||
try (TransactionExecutor executor = TransactionExecutor.initTransaction(datasource)) { | ||
try { | ||
T result = executable.execute(); | ||
executor.commit(); | ||
return result; | ||
} catch (Exception e) { | ||
executor.rollback(); | ||
throw new DataAccessException(); | ||
} | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
private interface ExecutableWithoutReturn { | ||
void execute(); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface ExecutableWithReturn<T> { | ||
T execute(); | ||
} | ||
|
||
private static class TransactionExecutor implements AutoCloseable { | ||
|
||
private final DataSource dataSource; | ||
private final Connection connection; | ||
|
||
private TransactionExecutor(DataSource dataSource, Connection connection) { | ||
this.dataSource = dataSource; | ||
this.connection = connection; | ||
} | ||
Comment on lines
+70
to
+78
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. 오... AutoCloseable에 이너클래스까지 ㄷㄷ |
||
|
||
public static TransactionExecutor initTransaction(DataSource dataSource) { | ||
try { | ||
Connection connection = DataSourceUtils.getConnection(dataSource); | ||
connection.setAutoCommit(false); | ||
return new TransactionExecutor(dataSource, connection); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
public void commit() { | ||
try { | ||
connection.commit(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
public void rollback() { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
DataSourceUtils.releaseConnection(dataSource); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,10 @@ | ||
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.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserService.class); | ||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public UserService(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) { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
Connection conn = null; | ||
try { | ||
conn = dataSource.getConnection(); | ||
conn.setAutoCommit(false); | ||
TransactionSynchronizationManager.bindResource(dataSource, conn); | ||
User user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
conn.commit(); | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
if (conn != null) { | ||
try { | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
conn.rollback(); | ||
conn.close(); | ||
} catch (SQLException ex) { | ||
log.error(ex.getMessage(), ex); | ||
} | ||
} | ||
throw new DataAccessException(); | ||
} | ||
} | ||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
Comment on lines
+7
to
+9
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. 사소한 부분인데요. 여태 모디가 직접 작성한 코드에서는 final이 없었던 것으로 기억하는데, 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. 이 부분은 주어진 뼈대 코드의 final인데 뼈대 코드를 완전히 수정하지 않아서 남아있네요 ㅎㅎ.. 불변성의 장점은 물론 알고 있어 예전에는 IDE의 도움으로 final을 붙여주었었는데, 자바 외의 언어를 썼다면 당연히 따랐겠지만 자바에서는 언제나 final 키워드를 붙이면서 오는 잔실수나 가독성 저하가 불변 키워드에서 오는 장점보다 더 크게 다가오더라구요. 또 캡슐화를 통한 불변 객체 구현이 아닌 메서드 내부에서의 파라미터와 지역 변수들의 final 키워드가 과연 유용한게 맞냐는 생각도 많이 했고요...ㅎㅎㅎ 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. 인정합니다 ㅎㅎ |
||
} |
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.
오태식이 돌아왔구나~
다시 돌아온 비즈니스 로직이 좋아요