-
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.
- Loading branch information
Showing
11 changed files
with
168 additions
and
88 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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import java.util.NoSuchElementException; | ||
|
||
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(NoSuchElementException::new); | ||
} | ||
|
||
@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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final TransactionTemplate transactionTemplate; | ||
private final UserService userService; | ||
|
||
public TxUserService(final TransactionTemplate transactionTemplate, final UserService userService) { | ||
this.transactionTemplate = transactionTemplate; | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(final Long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionTemplate.execute(() -> { | ||
userService.insert(user); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public void changePassword(final Long id, final String newPassword, final String createBy) { | ||
transactionTemplate.execute(() -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
}); | ||
} | ||
} |
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,69 +1,12 @@ | ||
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.dao.DataAccessException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
User findById(final Long id); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.dataSource = DataSourceConfig.getInstance(); | ||
} | ||
void insert(final User user); | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id) | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
Connection conn = null; | ||
try { | ||
conn = dataSource.getConnection(); | ||
conn.setAutoCommit(false); | ||
|
||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(dataSource.getConnection(), new UserHistory(user, createBy)); | ||
|
||
conn.commit(); | ||
} catch (final SQLException e) { | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
close(conn); | ||
} | ||
} | ||
|
||
private void rollback(final Connection conn) { | ||
try { | ||
conn.rollback(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException("fail rollback"); | ||
} | ||
} | ||
|
||
private void close(final Connection conn) { | ||
try { | ||
conn.close(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException("fail close"); | ||
} | ||
} | ||
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
7 changes: 7 additions & 0 deletions
7
jdbc/src/main/java/org/springframework/transaction/support/TransactionExecutor.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,7 @@ | ||
package org.springframework.transaction.support; | ||
|
||
@FunctionalInterface | ||
public interface TransactionExecutor<T> { | ||
|
||
T execute(); | ||
} |
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
58 changes: 58 additions & 0 deletions
58
jdbc/src/main/java/org/springframework/transaction/support/TransactionTemplate.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,58 @@ | ||
package org.springframework.transaction.support; | ||
|
||
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; | ||
|
||
public class TransactionTemplate { | ||
|
||
private final DataSource dataSource; | ||
|
||
public TransactionTemplate(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public <T> T execute(final TransactionExecutor<T> executor) { | ||
final Connection connection = beginTransaction(); | ||
try { | ||
final T result = executor.execute(); | ||
commit(connection); | ||
return result; | ||
} catch (final DataAccessException e) { | ||
rollback(connection); | ||
throw e; | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
|
||
private Connection beginTransaction() { | ||
final Connection connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e.getMessage(), e); | ||
} | ||
return connection; | ||
} | ||
|
||
private void commit(final Connection connection) { | ||
try { | ||
connection.commit(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private void rollback(final Connection connection) { | ||
try { | ||
connection.rollback(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e.getMessage(), e); | ||
} | ||
} | ||
} |