-
Notifications
You must be signed in to change notification settings - Fork 301
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
4 changed files
with
152 additions
and
64 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
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 org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.NoSuchElementException; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
|
||
public AppUserService( | ||
final UserDao userDao, | ||
final UserHistoryDao userHistoryDao, | ||
final DataSource dataSource | ||
) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.dataSource = dataSource; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) throws SQLException { | ||
return userDao.findById(dataSource.getConnection(), id) | ||
.orElseThrow(() -> new NoSuchElementException("해당 아이디의 사용자가 존재하지 않습니다.")); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
try { | ||
userDao.insert(conn, user); | ||
|
||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
try { | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
|
||
conn.commit(); | ||
} catch (SQLException | DataAccessException e) { | ||
System.out.println("e = " + e); | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final DataSource dataSource; | ||
private final UserService userService; | ||
|
||
public TxUserService( | ||
final DataSource dataSource, | ||
final UserService userService) { | ||
this.dataSource = dataSource; | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
try { | ||
final User user = userService.findById(id); | ||
|
||
conn.commit(); | ||
return user; | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
|
||
@Override | ||
public void insert(final User user) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
try { | ||
userService.insert(user); | ||
|
||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
try { | ||
userService.changePassword(id, newPassword, createBy); | ||
|
||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
} |
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,14 @@ | ||
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 org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.NoSuchElementException; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
private final DataSource dataSource; | ||
User findById(final long id) throws SQLException; | ||
|
||
public UserService( | ||
final UserDao userDao, | ||
final UserHistoryDao userHistoryDao, | ||
final DataSource dataSource | ||
) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
this.dataSource = dataSource; | ||
} | ||
void insert(final User user) throws SQLException; | ||
|
||
public User findById(final long id) throws SQLException { | ||
return userDao.findById(dataSource.getConnection(), id) | ||
.orElseThrow(() -> new NoSuchElementException("해당 아이디의 사용자가 존재하지 않습니다.")); | ||
} | ||
|
||
public void insert(final User user) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
try { | ||
userDao.insert(conn, user); | ||
|
||
conn.commit(); | ||
} catch (SQLException e) { | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
conn.setAutoCommit(false); | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
try { | ||
userDao.update(conn, user); | ||
userHistoryDao.log(conn, new UserHistory(user, createBy)); | ||
|
||
conn.commit(); | ||
} catch (SQLException | DataAccessException e) { | ||
System.out.println("e = " + e); | ||
conn.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy) throws SQLException; | ||
} |
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