-
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단계] 모디(전제희) 미션 제출합니다. (#597)
* refactor: JdbcTemplate이 UncheckedException을 외부로 위임하지 않도록 변경 * feat: TxUserService 구현 * refactor: JdbcTemplate의 close connection 부분 메서드 분리 * refactor: JdbcTemplate의 인스턴스화 로직 변경 * refactor: 사용되는 함수형 인터페이스 직접 정의 * refactor: connection 생성 삭제 책임을 DataSourceUtils로 집중
- Loading branch information
Showing
7 changed files
with
189 additions
and
116 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
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,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)); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
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,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; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
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,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); | ||
} |
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
22 changes: 0 additions & 22 deletions
22
jdbc/src/main/java/org/springframework/jdbc/core/InstantiateUtil.java
This file was deleted.
Oops, something went wrong.
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