Skip to content

Commit

Permalink
refactor: JdbcTemplate에서 DataAccessException을 반환하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
cookienc committed Sep 29, 2023
1 parent ba562ea commit 6f21f47
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -25,7 +26,7 @@ public JdbcTemplate(final DataSource dataSource) {
public void execute(final String sql, final Object... params) {
try (
final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql);
final PreparedStatement pstmt = conn.prepareStatement(sql)
) {
log.debug("query : {}", sql);

Expand All @@ -34,14 +35,14 @@ public void execute(final String sql, final Object... params) {
pstmt.executeUpdate();
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e);
}
}

public <T> List<T> query(final String sql, RowMapper<T> rowMapper) {
try (
final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql);
final PreparedStatement pstmt = conn.prepareStatement(sql)
) {
log.debug("query : {}", sql);

Expand All @@ -56,14 +57,14 @@ public <T> List<T> query(final String sql, RowMapper<T> rowMapper) {
return results;
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e);
}
}

public <T> Optional<T> queryForObject(final String sql, RowMapper<T> rowMapper, final Object... params) {
try (
final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql);
final PreparedStatement pstmt = conn.prepareStatement(sql)
) {
log.debug("query : {}", sql);

Expand All @@ -79,7 +80,7 @@ public <T> Optional<T> queryForObject(final String sql, RowMapper<T> rowMapper,
return Optional.empty();
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e);
}
}

Expand Down

0 comments on commit 6f21f47

Please sign in to comment.