-
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 라이브러리 구현하기 - 3,4단계] 스플릿(박상현) 미션 제출합니다. #537
Changes from 5 commits
684c5bf
c409d52
fcebdc8
da26b60
37154ec
a77d0c6
59178f5
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 |
---|---|---|
@@ -1,62 +1,30 @@ | ||
package com.techcourse.dao; | ||
|
||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.LocalDateTime; | ||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
public class UserHistoryDao { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class); | ||
|
||
private final DataSource dataSource; | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UserHistoryDao(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public UserHistoryDao(final JdbcTemplate jdbcTemplate) { | ||
this.dataSource = null; | ||
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) {} | ||
} | ||
final long userId = userHistory.getUserId(); | ||
final String account = userHistory.getAccount(); | ||
final String password = userHistory.getPassword(); | ||
final String email = userHistory.getEmail(); | ||
final LocalDateTime createdAt = userHistory.getCreatedAt(); | ||
final String createBy = userHistory.getCreateBy(); | ||
jdbcTemplate.update(sql, userId, account, password, email, createdAt, createBy); | ||
} | ||
} |
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(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.techcourse.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import org.springframework.jdbc.core.error.SqlExceptionConverter; | ||
|
||
public abstract class TransactionService<T> { | ||
|
||
protected T appService; | ||
|
||
protected TransactionService(final T appService) { | ||
this.appService = appService; | ||
} | ||
|
||
protected void rollback(final Connection connection) { | ||
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. try 문안에 try 문이 있는게 개인적으로 보기 불편하더라구요..ㅎㅎ |
||
try { | ||
connection.rollback(); | ||
} catch (SQLException e) { | ||
throw SqlExceptionConverter.convert(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 java.sql.Connection; | ||
import java.sql.SQLException; | ||
import org.springframework.jdbc.core.error.SqlExceptionConverter; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class TransactionUserService extends TransactionService<UserService> implements UserService { | ||
|
||
public TransactionUserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
super(new AppUserService(userDao, userHistoryDao)); | ||
} | ||
|
||
public User findById(final long id) { | ||
final Connection connection = DataSourceUtils.getConnection(DataSourceConfig.getInstance()); | ||
final User user = appService.findById(id); | ||
DataSourceUtils.releaseConnection(connection, DataSourceConfig.getInstance()); | ||
return user; | ||
} | ||
|
||
public void insert(final User user) { | ||
final Connection connection = DataSourceUtils.getConnection(DataSourceConfig.getInstance()); | ||
try { | ||
connection.setAutoCommit(false); | ||
appService.insert(user); | ||
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. appService 호출 부분을 제외하고 중복이라 이 부분 제거하면 매우 깔끔해지겠네요! TransactionSynchronizationManager 에서 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. 좋은 것 같습니다!! 반영했습니다!!👍 |
||
connection.commit(); | ||
} catch (SQLException e) { | ||
rollback(connection); | ||
throw SqlExceptionConverter.convert(e); | ||
} catch (Exception e) { | ||
rollback(connection); | ||
throw e; | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, DataSourceConfig.getInstance()); | ||
} | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final Connection connection = DataSourceUtils.getConnection(DataSourceConfig.getInstance()); | ||
try { | ||
connection.setAutoCommit(false); | ||
appService.changePassword(id, newPassword, createBy); | ||
connection.commit(); | ||
} catch (SQLException e) { | ||
rollback(connection); | ||
throw SqlExceptionConverter.convert(e); | ||
} catch (Exception e) { | ||
rollback(connection); | ||
throw e; | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, DataSourceConfig.getInstance()); | ||
} | ||
} | ||
} |
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); | ||
} |
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.
이 친구가 transaction 시작과 끝 관련 책임을 가지는 것도 좋아보이네요!! 어떻게 생각하시나요?