diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 52ff2fb8d5..8b408a569e 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,10 +1,8 @@ package com.techcourse.dao; -import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.User; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.datasource.DataSourceUtils; import java.util.List; @@ -35,7 +33,7 @@ public void update(final User user) { final var sql = "update users set account = ? , password = ? , email = ? where id = ?"; final Object[] parameters = {user.getAccount(), user.getPassword(), user.getEmail(), user.getId()}; - jdbcTemplate.update(DataSourceUtils.getConnection(DataSourceConfig.getInstance()), sql, parameters); + jdbcTemplate.update(sql, parameters); } public List findAll() { diff --git a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java index 093899c33a..7fa67a220e 100644 --- a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java +++ b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java @@ -1,9 +1,7 @@ package com.techcourse.dao; -import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.UserHistory; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceUtils; public class UserHistoryDao { @@ -17,6 +15,6 @@ public void log(final UserHistory userHistory) { final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)"; final Object[] parameters = {userHistory.getUserId(), userHistory.getAccount(), userHistory.getPassword(), userHistory.getEmail(), userHistory.getCreatedAt(), userHistory.getCreateBy()}; - jdbcTemplate.update(DataSourceUtils.getConnection(DataSourceConfig.getInstance()), sql, parameters); + jdbcTemplate.update(sql, parameters); } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index c482069f86..ded4ab0334 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -3,6 +3,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.datasource.DataSourceUtils; import javax.sql.DataSource; import java.sql.Connection; @@ -23,57 +24,37 @@ public JdbcTemplate(final DataSource dataSource) { } public int update(final String sql, final Object... parameters) { - try (Connection conn = dataSource.getConnection(); - PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters) - ) { - log.debug("Execute Update - query : {}", sql); - return pstmt.executeUpdate(); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new DataAccessException(e); - } + return executeQuery(sql, PreparedStatement::executeUpdate, parameters); } - public int update(final Connection conn, final String sql, final Object[] parameters) { - try (PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)){ - log.debug("Execute Update - query : {}", sql); - return pstmt.executeUpdate(); + private T executeQuery(final String sql, final PreparedStateExecutor executor, final Object... parameters) { + final Connection conn = DataSourceUtils.getConnection(dataSource); + try (PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)) { + log.debug("Execute - query : {}", sql); + return executor.execute(pstmt); } catch (SQLException e) { + log.error(e.getMessage(), e); throw new DataAccessException(e); + } finally { + DataSourceUtils.releaseConnection(conn, dataSource); } } public T queryForObject(final String sql, final RowMapper rowMapper, final Object... parameters) { - try (Connection conn = dataSource.getConnection(); - PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters); - ResultSet rs = pstmt.executeQuery() - ) { - log.debug("Execute Query -query : {}", sql); - return SingleResult.makeSingleResultFrom(makeResults(rowMapper, rs)); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new DataAccessException(e); - } + return executeQuery(sql, preparedStatement -> SingleResult.makeSingleResultFrom(makeResults(rowMapper, preparedStatement)), parameters); } - private List makeResults(final RowMapper rowMapper, final ResultSet rs) throws SQLException { + private List makeResults(final RowMapper rowMapper, final PreparedStatement preparedStatement) throws SQLException { List list = new ArrayList<>(); - while (rs.next()) { - list.add(rowMapper.mapping(rs)); + try (final ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + list.add(rowMapper.mapping(resultSet)); + } } return list; } - public List queryForList(final String sql, final RowMapper rowMapper) { - try (Connection conn = dataSource.getConnection(); - PreparedStatement pstmt = conn.prepareStatement(sql); - ResultSet rs = pstmt.executeQuery() - ) { - log.debug("Execute Query - query : {}", sql); - return makeResults(rowMapper, rs); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new DataAccessException(e); - } + public List queryForList(final String sql, final RowMapper rowMapper, final Object... parameters) { + return executeQuery(sql, preparedStatement -> makeResults(rowMapper, preparedStatement), parameters); } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStateExecutor.java b/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStateExecutor.java new file mode 100644 index 0000000000..aeeafd3772 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStateExecutor.java @@ -0,0 +1,9 @@ +package org.springframework.jdbc.core; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +public interface PreparedStateExecutor { + + T execute(final PreparedStatement preparedStatement) throws SQLException; +}