-
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단계] 말랑(신동훈) 미션 제출합니다. #558
Changes from 7 commits
971a7f0
f0480cf
6031428
73ab509
0edff56
770439a
9154149
60d5a00
8864d98
ad8eeae
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) { | ||
final var 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,29 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TxTemplate; | ||
|
||
public class TxTemplateUserService implements UserService { | ||
|
||
private final UserService delegate; | ||
private final TxTemplate txTemplate; | ||
|
||
public TxTemplateUserService(UserService delegate, TxTemplate txTemplate) { | ||
this.delegate = delegate; | ||
this.txTemplate = txTemplate; | ||
} | ||
|
||
public User findById(final long id) { | ||
return delegate.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
delegate.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
txTemplate.execute(() -> { | ||
delegate.changePassword(id, newPassword, createBy); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 UserService delegate; | ||
|
||
public TxUserService(UserService delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public User findById(final long id) { | ||
return delegate.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
delegate.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
try (Connection conn = DataSourceUtils.getConnection(dataSource)) { | ||
try { | ||
conn.setAutoCommit(false); | ||
delegate.changePassword(id, newPassword, createBy); | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
shin-mallang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +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; | ||
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) { | ||
DataSource dataSource = DataSourceConfig.getInstance(); | ||
try (Connection conn = dataSource.getConnection()) { | ||
try { | ||
conn.setAutoCommit(false); | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
conn.commit(); | ||
} catch (Exception e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
package org.springframework.transaction.support; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import javax.sql.DataSource; | ||
|
||
public abstract class TransactionSynchronizationManager { | ||
|
||
private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>(); | ||
|
||
private TransactionSynchronizationManager() {} | ||
private TransactionSynchronizationManager() { | ||
} | ||
|
||
@Nullable | ||
public static Connection getResource(DataSource key) { | ||
return null; | ||
Map<DataSource, Connection> dataSourceConnectionMap = resources.get(); | ||
if (dataSourceConnectionMap == null) { | ||
return null; | ||
} | ||
return dataSourceConnectionMap.get(key); | ||
} | ||
|
||
public static void bindResource(DataSource key, Connection value) { | ||
resources.set(new HashMap<>() {{ | ||
put(key, value); | ||
}}); | ||
} | ||
|
||
public static Connection unbindResource(DataSource key) { | ||
return null; | ||
Map<DataSource, Connection> dataSourceConnectionMap = resources.get(); | ||
Connection remove = dataSourceConnectionMap.remove(key); | ||
resources.remove(); | ||
return remove; | ||
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. 이 부분 제가 잘 몰라서 그러는데, 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.
|
||
} | ||
} |
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.
오홍 네이밍을 delegate로 하셨는데
어떨 때 이런 네이밍을 사용하는지 알려주실 수 있나요.? 처음 봐서요..!
구체적인 작업은 UserService가 하도록 위임한다는 뜻인가요?
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.
음 사실 TxUserService에서 실제 로직은 다른 서비스에 위임하기 때문에, 이를 표현하기 위해서 이렇게 네이밍 했습니다!