Skip to content
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 구현하기 - 2단계] 하디(전동혁) 미션 제출합니다. #353

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/src/test/java/com/techcourse/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import com.techcourse.config.DataSourceConfig;
import com.techcourse.domain.User;
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static org.assertj.core.api.Assertions.assertThat;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserDaoTest {

private UserDao userDao;

@BeforeEach
@BeforeAll
void setup() {
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance());

Expand Down
84 changes: 31 additions & 53 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class JdbcTemplate {

Expand All @@ -24,32 +25,15 @@ public JdbcTemplate(final DataSource dataSource) {
}

public void update(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
preparedStatement = getPreparedStatement(connection, sql, args);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DatabaseResourceException("Timeout or PreparedStatement is already closed", e);
} finally {
closeResources(connection, preparedStatement, resultSet);
}
template(this::executeUpdate, sql, args);
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
preparedStatement = getPreparedStatement(connection, sql, args);
resultSet = getResultSet(preparedStatement);
Function<PreparedStatement, List<T>> query = preparedStatement -> {
ResultSet resultSet = executeQuery(preparedStatement);
return getObjects(resultSet, rowMapper);
} finally {
closeResources(connection, preparedStatement, resultSet);
}
};
return template(query, sql, args);
}

public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
Expand All @@ -63,27 +47,16 @@ public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
return results.get(0);
}

private Connection getConnection() {
private int executeUpdate(PreparedStatement preparedStatement) {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new DatabaseResourceException("Connection cannot be acquired.", e);
}
}

private PreparedStatement getPreparedStatement(Connection connection, String sql, Object... args) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int idx = 1; idx <= args.length; idx++) {
preparedStatement.setObject(idx, args[idx - 1]);
}
return preparedStatement;
return preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DatabaseResourceException("PreparedStatement cannot be acquired.", e);
throw new DatabaseResourceException(
"Database access error.", e);
}
}

private ResultSet getResultSet(PreparedStatement preparedStatement) {
private ResultSet executeQuery(PreparedStatement preparedStatement) {
try {
return preparedStatement.executeQuery();
} catch (SQLException e) {
Expand All @@ -105,28 +78,33 @@ private <T> List<T> getObjects(ResultSet resultSet, RowMapper<T> rowMapper) {
return results;
}

private void closeResources(Connection connection,
PreparedStatement preparedStatement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException ignored) {
private <T> T template(Function<PreparedStatement, T> function, String sql, Object... args) {
try (
Connection connection = getConnection();
PreparedStatement preparedStatement = getPreparedStatement(connection, sql, args)) {
return function.apply(preparedStatement);
} catch (SQLException e) {
throw new IllegalArgumentException();
}
}

private Connection getConnection() {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {
return dataSource.getConnection();
} catch (SQLException e) {
throw new DatabaseResourceException("Connection cannot be acquired.", e);
}
}

private PreparedStatement getPreparedStatement(Connection connection, String sql, Object... args) {
try {
if (connection != null) {
connection.close();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int idx = 1; idx <= args.length; idx++) {
preparedStatement.setObject(idx, args[idx - 1]);
}
} catch (SQLException ignored) {
return preparedStatement;
} catch (SQLException e) {
throw new DatabaseResourceException("PreparedStatement cannot be acquired.", e);
}
}
}
Loading