-
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단계] 디노(신종화) 미션 제출합니다. (#543)
* refactor: Transaction synchronization 적용 * refactor: Transaction Service 추상화 * refactor: 패키지 구조 변경 및 Executor 추상화 * refactor: null 처리 일관성있게 변경 * refactor: resource 미리 초기화 및 unbind시 remove 하도록 변경 * refactor: query와 queryForObject에서도 동일한 connection을 사용하도록 변경 * refactor: 네이밍 변경
- Loading branch information
Showing
12 changed files
with
169 additions
and
97 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
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,59 +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; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
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 DataSource dataSource = DataSourceConfig.getInstance(); | ||
Connection conn = null; | ||
try { | ||
conn = dataSource.getConnection(); | ||
conn.setAutoCommit(false); | ||
|
||
final User user = findById(id); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
try { | ||
conn.rollback(); | ||
} catch (SQLException exception) { | ||
throw new DataAccessException(exception); | ||
} | ||
} finally { | ||
try { | ||
conn.close(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/techcourse/service/impl/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.impl; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import com.techcourse.service.UserService; | ||
|
||
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(long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/techcourse/service/impl/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,35 @@ | ||
package com.techcourse.service.impl; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.service.UserService; | ||
import org.springframework.transaction.support.ServiceExecutor; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final ServiceExecutor serviceExecutor; | ||
|
||
public TxUserService(final UserService userService) { | ||
this.userService = userService; | ||
this.serviceExecutor = new ServiceExecutor(DataSourceConfig.getInstance()); | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userService.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
serviceExecutor.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
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
40 changes: 40 additions & 0 deletions
40
jdbc/src/main/java/org/springframework/transaction/support/ServiceExecutor.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,40 @@ | ||
package org.springframework.transaction.support; | ||
|
||
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 ServiceExecutor { | ||
|
||
private final DataSource dataSource; | ||
|
||
public ServiceExecutor(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public <T> T execute(TransactionTemplate<T> executor) { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
conn.setAutoCommit(false); | ||
final T result = executor.execute(); | ||
conn.commit(); | ||
return result; | ||
} catch (Exception e) { | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
|
||
private void rollback(final Connection conn) { | ||
try { | ||
conn.rollback(); | ||
} catch (SQLException exception) { | ||
throw new DataAccessException(exception); | ||
} | ||
} | ||
} |
Oops, something went wrong.