Skip to content

Commit

Permalink
feat: try with resources로 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
younghoondoodoom committed Sep 27, 2023
1 parent 58e71c1 commit 5845ab3
Showing 1 changed file with 33 additions and 90 deletions.
123 changes: 33 additions & 90 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -22,122 +21,66 @@ public JdbcTemplate(final DataSource dataSource) {
}

public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
for (int parameterIndex = 1; parameterIndex < parameters.length + 1; parameterIndex++) {
pstmt.setObject(parameterIndex, parameters[parameterIndex - 1]);
}
rs = pstmt.executeQuery();
try (final var conn = dataSource.getConnection();
final var pstmt = conn.prepareStatement(sql)) {

log.debug("query : {}", sql);
setObjectToPreparedStatement(pstmt, parameters);

return rs.next() ? Optional.of(rowMapper.map(rs)) : Optional.empty();
return getObject(sql, rowMapper, pstmt);
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (final SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (final SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (final SQLException ignored) {
}
}
}

private void setObjectToPreparedStatement(final PreparedStatement pstmt, final Object[] parameters) throws SQLException {
for (int parameterIndex = 1; parameterIndex < parameters.length + 1; parameterIndex++) {
pstmt.setObject(parameterIndex, parameters[parameterIndex - 1]);
}
}

private <T> Optional<T> getObject(final String sql, final RowMapper<T> rowMapper, final PreparedStatement pstmt) throws SQLException {
try (final ResultSet rs = pstmt.executeQuery()) {
log.debug("query : {}", sql);
return rs.next() ? Optional.of(rowMapper.map(rs)) : Optional.empty();
}
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
try (final var conn = dataSource.getConnection();
final var pstmt = conn.prepareStatement(sql);
final var rs = pstmt.executeQuery();) {

log.debug("query : {}", sql);

final List<T> result = new ArrayList<>();
while (rs.next()) {
final T data = rowMapper.map(rs);
result.add(data);
}
return result;
return getObjects(rowMapper, rs);
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (final SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (final SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (final SQLException ignored) {
}
}
}

private <T> List<T> getObjects(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException {
final List<T> result = new ArrayList<>();
while (rs.next()) {
final T data = rowMapper.map(rs);
result.add(data);
}
return result;
}

public void update(final String sql, final Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
try (final var conn = dataSource.getConnection();
final var pstmt = conn.prepareStatement(sql);) {

log.debug("query : {}", sql);

for (int parameterIndex = 1; parameterIndex < parameters.length + 1; parameterIndex++) {
pstmt.setObject(parameterIndex, parameters[parameterIndex - 1]);
}
setObjectToPreparedStatement(pstmt, parameters);

pstmt.executeUpdate();
} catch (final SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (final SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (final SQLException ignored) {
}
}
}
}

0 comments on commit 5845ab3

Please sign in to comment.