-
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 라이브러리 구현하기 - 3,4단계] 애쉬(정설희) 미션 제출합니다. (#589)
* feat: Transaction 적용하기 * refactor: TxSynchronizationManager 활용 * refactor: 트랜잭션 서비스 추상화 * refactor: transaction 일괄 적용 * refactor: 트랜잭션 종료 후 setAutoCommit(true) 설정
- Loading branch information
Showing
11 changed files
with
188 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,20 @@ | ||
package com.techcourse.dao; | ||
|
||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
public class UserHistoryDao { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class); | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private final DataSource dataSource; | ||
|
||
public UserHistoryDao(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public UserHistoryDao(final JdbcTemplate jdbcTemplate) { | ||
this.dataSource = null; | ||
public UserHistoryDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void log(final UserHistory userHistory) { | ||
final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)"; | ||
|
||
Connection conn = null; | ||
PreparedStatement pstmt = null; | ||
try { | ||
conn = dataSource.getConnection(); | ||
pstmt = conn.prepareStatement(sql); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
pstmt.setLong(1, userHistory.getUserId()); | ||
pstmt.setString(2, userHistory.getAccount()); | ||
pstmt.setString(3, userHistory.getPassword()); | ||
pstmt.setString(4, userHistory.getEmail()); | ||
pstmt.setObject(5, userHistory.getCreatedAt()); | ||
pstmt.setString(6, userHistory.getCreateBy()); | ||
pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if (pstmt != null) { | ||
pstmt.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
|
||
try { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
jdbcTemplate.update(sql, userHistory.getUserId(), userHistory.getAccount(), userHistory.getPassword(), | ||
userHistory.getEmail(), userHistory.getCreatedAt(), userHistory.getCreateBy()); | ||
} | ||
} |
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(UserDao userDao, UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(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)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
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(UserService userService, TransactionManager transactionManager) { | ||
this.userService = userService; | ||
this.transactionManager = transactionManager; | ||
} | ||
|
||
@Override | ||
public User findById(long id) { | ||
return transactionManager.doTransaction(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
transactionManager.doTransaction(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.doTransaction(() -> 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,32 +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; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
User findById(final long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
void insert(final User user); | ||
|
||
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)); | ||
} | ||
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
11 changes: 5 additions & 6 deletions
11
jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.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
55 changes: 55 additions & 0 deletions
55
jdbc/src/main/java/org/springframework/transaction/TransactionManager.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,55 @@ | ||
package org.springframework.transaction; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.function.Supplier; | ||
import javax.sql.DataSource; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class TransactionManager { | ||
|
||
private final DataSource dataSource; | ||
|
||
public TransactionManager(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public <T> T doTransaction(Supplier<T> transactionExecutor) { | ||
return executeTransaction(transactionExecutor); | ||
} | ||
|
||
public void doTransaction(Runnable transactionExecutor) { | ||
executeTransaction(() -> { | ||
transactionExecutor.run(); | ||
return null; | ||
}); | ||
} | ||
|
||
private <T> T executeTransaction(Supplier<T> transactionExecutor) { | ||
T result = null; | ||
try (final var conn = DataSourceUtils.getConnection(dataSource)) { | ||
TransactionSynchronizationManager.bindResource(dataSource, conn); | ||
conn.setAutoCommit(false); | ||
result = transactionExecutor.get(); | ||
conn.commit(); | ||
conn.setAutoCommit(true); | ||
} catch (final SQLException e) { | ||
rollBack(e, TransactionSynchronizationManager.getResource(dataSource)); | ||
} finally { | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
return result; | ||
} | ||
|
||
private void rollBack(final SQLException e, final Connection conn) { | ||
try { | ||
conn.rollback(); | ||
conn.setAutoCommit(true); | ||
throw new DataAccessException(e); | ||
} catch (final SQLException exception) { | ||
throw new DataAccessException(exception); | ||
} | ||
} | ||
} |
Oops, something went wrong.