From b8199462a55a2d983b647549714c2d0e4a735778 Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 04:37:15 +0900 Subject: [PATCH 1/7] =?UTF-8?q?test:=20connection=20pool=20=ED=95=99?= =?UTF-8?q?=EC=8A=B5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=99=84=EB=A3=8C=20?= =?UTF-8?q?(=EA=B4=80=EB=A0=A8=20=EC=A3=BC=EC=84=9D=20=EC=9E=91=EC=84=B1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PoolingVsNoPoolingTest.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java b/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java index 20942cca11..9025e36b9f 100644 --- a/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java +++ b/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java @@ -68,28 +68,41 @@ void noPoling() throws SQLException { connect(dataSource); long end = ClockSource.currentTime(); - // 테스트 결과를 확인한다. + // 테스트 결과를 확인한다. : Elapsed runtime: 4s615ms log.info("Elapsed runtime: {}", ClockSource.elapsedDisplayString(start, end)); } @Test void pooling() throws SQLException { + // PostgreSQL 에서 제공하는 공식 connections = ((core_count * 2) + effective_spindle_count)을 참고 final var config = new HikariConfig(); config.setJdbcUrl(container.getJdbcUrl()); config.setUsername(container.getUsername()); config.setPassword(container.getPassword()); config.setMinimumIdle(1); - config.setMaximumPoolSize(1); + config.setMaximumPoolSize(16); // 내 맥북 코어 개수 *2 함. 이 값만 기본에서 바꿨을 때 755~721ms config.setConnectionTimeout(1000); config.setAutoCommit(false); - config.setReadOnly(false); + config.setReadOnly(true); // 이 값만 기본에서 바꿨을 때 745ms + // 추가 + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.addDataSourceProperty("useServerPrepStmts", "true"); + config.addDataSourceProperty("useLocalSessionState", "true"); + config.addDataSourceProperty("rewriteBatchedStatements", "true"); + config.addDataSourceProperty("cacheResultSetMetadata", "true"); + config.addDataSourceProperty("cacheServerConfiguration", "true"); + config.addDataSourceProperty("elideSetAutoCommits", "true"); + config.addDataSourceProperty("maintainTimeStats", "false"); final var hikariDataSource = new HikariDataSource(config); long start = ClockSource.currentTime(); connect(hikariDataSource); long end = ClockSource.currentTime(); - // 테스트 결과를 확인한다. + // 테스트 결과를 확인한다. 기본 값에 따르면 Elapsed runtime: 772ms, 위 설정값에 따르면 720ms + // 근데 오차범위가 너무 큰 것 같다. log.info("Elapsed runtime: {}", ClockSource.elapsedDisplayString(start, end)); } From 58d322cdabb2cc4584f97862b2df9c6f6878cbf7 Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 05:35:01 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20ConnectionManager=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../init/DataSourceConnectionManager.java | 22 +++++++++++++++++++ .../jdbc/core/ConnectionManager.java | 11 ++++++++++ 2 files changed, 33 insertions(+) create mode 100644 app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java create mode 100644 jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java diff --git a/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java b/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java new file mode 100644 index 0000000000..ddcf77e1ad --- /dev/null +++ b/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java @@ -0,0 +1,22 @@ +package com.techcourse.support.jdbc.init; + +import com.techcourse.config.DataSourceConfig; +import org.springframework.jdbc.CannotGetJdbcConnectionException; +import org.springframework.jdbc.core.ConnectionManager; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +public class DataSourceConnectionManager implements ConnectionManager { + + public Connection getConnection() throws CannotGetJdbcConnectionException { + try { + DataSource dataSource = DataSourceConfig.getInstance(); + return dataSource.getConnection(); + } catch (final SQLException exception) { + throw new CannotGetJdbcConnectionException("Datasource connection error:" + exception.getMessage()); + } + } + +} diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java b/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java new file mode 100644 index 0000000000..8a8d0d45e6 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java @@ -0,0 +1,11 @@ +package org.springframework.jdbc.core; + +import org.springframework.jdbc.CannotGetJdbcConnectionException; + +import java.sql.Connection; + +public interface ConnectionManager { + + Connection getConnection() throws CannotGetJdbcConnectionException; + +} From 1cffb1e3cd091c11b1d02839f2fc0a00f281a3ae Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 05:36:42 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20JdbcTemplate,=20RowMapper=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/SqlQueryException.java | 12 +++++ .../jdbc/core/JdbcTemplate.java | 48 +++++++++++++++++-- .../springframework/jdbc/core/RowMapper.java | 11 +++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 jdbc/src/main/java/org/springframework/SqlQueryException.java create mode 100644 jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java diff --git a/jdbc/src/main/java/org/springframework/SqlQueryException.java b/jdbc/src/main/java/org/springframework/SqlQueryException.java new file mode 100644 index 0000000000..e315b0ce00 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/SqlQueryException.java @@ -0,0 +1,12 @@ +package org.springframework; + +public class SqlQueryException extends RuntimeException { + + private static final String WRONG_QUERY_ERROR_MESSAGE_FORMAT = "SQL Query error: %s \nSQL: %s"; + + public SqlQueryException(final String message, final String query) { + super(String.format(WRONG_QUERY_ERROR_MESSAGE_FORMAT, message, query)); + } + + +} 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 52a0d30a17..d75d6db724 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -2,16 +2,56 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.SqlQueryException; -import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; public class JdbcTemplate { private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); - private final DataSource dataSource; + private final ConnectionManager connectionManager; - public JdbcTemplate(final DataSource dataSource) { - this.dataSource = dataSource; + public JdbcTemplate(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; } + + public void executeUpdate(String query, List parameters) { + try (final Connection connection = connectionManager.getConnection(); + final PreparedStatement preparedStatement = connection.prepareStatement(query)) { + log.info("query: {}", query); + setParameters(preparedStatement, parameters); + preparedStatement.executeUpdate(); + } catch (SQLException exception) { + throw new SqlQueryException(exception.getMessage(), query); + } + } + + public T executeQuery(String query, List parameters, RowMapper rowMapper) { + try (final Connection connection = connectionManager.getConnection(); + final PreparedStatement preparedStatement = connection.prepareStatement(query); + final ResultSet resultSet = executePreparedStatementQuery(preparedStatement, parameters)) { + log.info("query: {}", query); + return rowMapper.mapRow(resultSet); + } catch (SQLException exception) { + throw new SqlQueryException(exception.getMessage(), query); + } + } + + private ResultSet executePreparedStatementQuery(PreparedStatement preparedStatement, List parameters) + throws SQLException { + setParameters(preparedStatement, parameters); + return preparedStatement.executeQuery(); + } + + private void setParameters(PreparedStatement preparedStatement, List parameters) throws SQLException { + for (int index = 1; index <= parameters.size(); index++) { + preparedStatement.setString(index, String.valueOf(parameters.get(index - 1))); + } + } + } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java new file mode 100644 index 0000000000..9d010ae406 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java @@ -0,0 +1,11 @@ +package org.springframework.jdbc.core; + +import java.sql.ResultSet; +import java.sql.SQLException; + +@FunctionalInterface +public interface RowMapper { + + T mapRow(ResultSet resultSet) throws SQLException; + +} From 1b6b5fda24f5cb2b3070a80f4d1c83f840836dc3 Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 05:37:43 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20JdbcTemplate=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20UserDao,=20UserHistoryDao=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 136 ++++++------------ .../com/techcourse/dao/UserHistoryDao.java | 56 ++------ .../java/com/techcourse/dao/UserDaoTest.java | 4 +- .../techcourse/service/UserServiceTest.java | 4 +- 4 files changed, 66 insertions(+), 134 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index d14c545f34..202ef3cc17 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,121 +1,79 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); + private static final RowMapper USER_ROW_MAPPER = resultSet -> { + if (resultSet.next()) { + return new User( + resultSet.getLong(1), + resultSet.getString(2), + resultSet.getString(3), + resultSet.getString(4) + ); + } + return null; + }; + private static final RowMapper> USERS_ROW_MAPPER = resultSet -> { + List users = new ArrayList<>(); + while (resultSet.next()) { + users.add(new User( + resultSet.getLong(1), + resultSet.getString(2), + resultSet.getString(3), + resultSet.getString(4) + )); + } + return users; + }; - private final DataSource dataSource; - - public UserDao(final DataSource dataSource) { - this.dataSource = dataSource; - } + private final JdbcTemplate jdbcTemplate; - public UserDao(final JdbcTemplate jdbcTemplate) { - this.dataSource = null; + public UserDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; } public void insert(final User user) { final var sql = "insert into users (account, password, email) values (?, ?, ?)"; - - Connection conn = null; - PreparedStatement pstmt = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - - log.debug("query : {}", sql); - - pstmt.setString(1, user.getAccount()); - pstmt.setString(2, user.getPassword()); - pstmt.setString(3, user.getEmail()); - pstmt.executeUpdate(); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } finally { - try { - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) {} - - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) {} - } + jdbcTemplate.executeUpdate(sql, List.of( + user.getAccount(), + user.getPassword(), + user.getEmail() + )); } public void update(final User user) { - // todo + final var sql = "update users set account = ?, password = ?, email = ? where id = ?"; + jdbcTemplate.executeUpdate(sql, List.of( + user.getAccount(), + user.getPassword(), + user.getEmail(), + user.getId() + )); } public List findAll() { - // todo - return null; + final var sql = "select id, account, password, email from users"; + return jdbcTemplate.executeQuery(sql, Collections.emptyList(), USERS_ROW_MAPPER); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; - - Connection conn = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - pstmt.setLong(1, id); - rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - if (rs.next()) { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); - } - return null; - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } finally { - try { - if (rs != null) { - rs.close(); - } - } catch (SQLException ignored) {} - - try { - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) {} - - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) {} - } + return jdbcTemplate.executeQuery(sql, List.of(id), USER_ROW_MAPPER); } public User findByAccount(final String account) { - // todo - return null; + final var sql = "select id, account, password, email from users where account = ?"; + return jdbcTemplate.executeQuery(sql, List.of(account), USER_ROW_MAPPER); } } diff --git a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java index edb4338caa..da26160daf 100644 --- a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java +++ b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java @@ -1,62 +1,32 @@ package com.techcourse.dao; import com.techcourse.domain.UserHistory; -import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; +import java.util.List; public class UserHistoryDao { private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class); - private final DataSource dataSource; - - public UserHistoryDao(final DataSource dataSource) { - this.dataSource = dataSource; - } + private final JdbcTemplate jdbcTemplate; - public UserHistoryDao(final JdbcTemplate jdbcTemplate) { - this.dataSource = null; + public UserHistoryDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; } public void log(final UserHistory userHistory) { final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)"; - Connection conn = null; - PreparedStatement pstmt = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - - log.debug("query : {}", sql); - - pstmt.setLong(1, userHistory.getUserId()); - pstmt.setString(2, userHistory.getAccount()); - pstmt.setString(3, userHistory.getPassword()); - pstmt.setString(4, userHistory.getEmail()); - pstmt.setObject(5, userHistory.getCreatedAt()); - pstmt.setString(6, userHistory.getCreateBy()); - pstmt.executeUpdate(); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } finally { - try { - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) {} - - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) {} - } + jdbcTemplate.executeUpdate(sql, List.of( + userHistory.getUserId(), + userHistory.getAccount(), + userHistory.getPassword(), + userHistory.getEmail(), + userHistory.getCreatedAt(), + userHistory.getCreateBy() + )); } } diff --git a/app/src/test/java/com/techcourse/dao/UserDaoTest.java b/app/src/test/java/com/techcourse/dao/UserDaoTest.java index 773d7faf82..3a95053d8a 100644 --- a/app/src/test/java/com/techcourse/dao/UserDaoTest.java +++ b/app/src/test/java/com/techcourse/dao/UserDaoTest.java @@ -2,9 +2,11 @@ import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.User; +import com.techcourse.support.jdbc.init.DataSourceConnectionManager; import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.jdbc.core.JdbcTemplate; import static org.assertj.core.api.Assertions.assertThat; @@ -16,7 +18,7 @@ class UserDaoTest { void setup() { DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); - userDao = new UserDao(DataSourceConfig.getInstance()); + userDao = new UserDao(new JdbcTemplate(new DataSourceConnectionManager())); final var user = new User("gugu", "password", "hkkang@woowahan.com"); userDao.insert(user); } diff --git a/app/src/test/java/com/techcourse/service/UserServiceTest.java b/app/src/test/java/com/techcourse/service/UserServiceTest.java index 255a0ebfe7..c4ef89b995 100644 --- a/app/src/test/java/com/techcourse/service/UserServiceTest.java +++ b/app/src/test/java/com/techcourse/service/UserServiceTest.java @@ -4,8 +4,10 @@ import com.techcourse.dao.UserDao; import com.techcourse.dao.UserHistoryDao; import com.techcourse.domain.User; +import com.techcourse.support.jdbc.init.DataSourceConnectionManager; import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.ConnectionManager; import org.springframework.jdbc.core.JdbcTemplate; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -22,7 +24,7 @@ class UserServiceTest { @BeforeEach void setUp() { - this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); + this.jdbcTemplate = new JdbcTemplate(new DataSourceConnectionManager()); this.userDao = new UserDao(jdbcTemplate); DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); From f7b954cb51161fc1ddfbd3d3513bcd846e5c679d Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 05:44:04 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20JdbcTemplate=20=EC=9D=B8?= =?UTF-8?q?=EC=9E=90=EB=A1=9C=20List=20=EB=8C=80=EC=8B=A0=20varargs=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 18 +++++++----------- .../com/techcourse/dao/UserHistoryDao.java | 8 +++----- .../jdbc/core/JdbcTemplate.java | 14 ++++++-------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 202ef3cc17..05b5c390ef 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,18 +1,14 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class UserDao { - private static final Logger log = LoggerFactory.getLogger(UserDao.class); private static final RowMapper USER_ROW_MAPPER = resultSet -> { if (resultSet.next()) { return new User( @@ -45,35 +41,35 @@ public UserDao(JdbcTemplate jdbcTemplate) { public void insert(final User user) { final var sql = "insert into users (account, password, email) values (?, ?, ?)"; - jdbcTemplate.executeUpdate(sql, List.of( + jdbcTemplate.executeUpdate(sql, user.getAccount(), user.getPassword(), user.getEmail() - )); + ); } public void update(final User user) { final var sql = "update users set account = ?, password = ?, email = ? where id = ?"; - jdbcTemplate.executeUpdate(sql, List.of( + jdbcTemplate.executeUpdate(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId() - )); + ); } public List findAll() { final var sql = "select id, account, password, email from users"; - return jdbcTemplate.executeQuery(sql, Collections.emptyList(), USERS_ROW_MAPPER); + return jdbcTemplate.executeQuery(sql, USERS_ROW_MAPPER); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; - return jdbcTemplate.executeQuery(sql, List.of(id), USER_ROW_MAPPER); + return jdbcTemplate.executeQuery(sql, USER_ROW_MAPPER, id); } public User findByAccount(final String account) { final var sql = "select id, account, password, email from users where account = ?"; - return jdbcTemplate.executeQuery(sql, List.of(account), USER_ROW_MAPPER); + return jdbcTemplate.executeQuery(sql, USER_ROW_MAPPER, account); } } diff --git a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java index da26160daf..6efe3b7cf3 100644 --- a/app/src/main/java/com/techcourse/dao/UserHistoryDao.java +++ b/app/src/main/java/com/techcourse/dao/UserHistoryDao.java @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; -import java.util.List; - public class UserHistoryDao { private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class); @@ -19,14 +17,14 @@ public UserHistoryDao(JdbcTemplate jdbcTemplate) { public void log(final UserHistory userHistory) { final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)"; - - jdbcTemplate.executeUpdate(sql, List.of( + log.info("query: {}", sql); + jdbcTemplate.executeUpdate(sql, userHistory.getUserId(), userHistory.getAccount(), userHistory.getPassword(), userHistory.getEmail(), userHistory.getCreatedAt(), userHistory.getCreateBy() - )); + ); } } 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 d75d6db724..f80cf39170 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -8,7 +8,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.List; public class JdbcTemplate { @@ -20,7 +19,7 @@ public JdbcTemplate(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void executeUpdate(String query, List parameters) { + public void executeUpdate(String query, Object... parameters) { try (final Connection connection = connectionManager.getConnection(); final PreparedStatement preparedStatement = connection.prepareStatement(query)) { log.info("query: {}", query); @@ -31,7 +30,7 @@ public void executeUpdate(String query, List parameters) { } } - public T executeQuery(String query, List parameters, RowMapper rowMapper) { + public T executeQuery(String query, RowMapper rowMapper, Object... parameters) { try (final Connection connection = connectionManager.getConnection(); final PreparedStatement preparedStatement = connection.prepareStatement(query); final ResultSet resultSet = executePreparedStatementQuery(preparedStatement, parameters)) { @@ -42,15 +41,14 @@ public T executeQuery(String query, List parameters, RowMapper ro } } - private ResultSet executePreparedStatementQuery(PreparedStatement preparedStatement, List parameters) - throws SQLException { + private ResultSet executePreparedStatementQuery(PreparedStatement preparedStatement, Object... parameters) throws SQLException { setParameters(preparedStatement, parameters); return preparedStatement.executeQuery(); } - private void setParameters(PreparedStatement preparedStatement, List parameters) throws SQLException { - for (int index = 1; index <= parameters.size(); index++) { - preparedStatement.setString(index, String.valueOf(parameters.get(index - 1))); + private void setParameters(PreparedStatement preparedStatement, Object... parameters) throws SQLException { + for (int index = 1; index <= parameters.length; index++) { + preparedStatement.setString(index, String.valueOf(parameters[index - 1])); } } From b2ab9fa8ccf5b1fd347facf56f764ceb5e949981 Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 05:47:37 +0900 Subject: [PATCH 6/7] refactor: optimize imports --- .../AppWebApplicationInitializer.java | 6 +++--- .../main/java/com/techcourse/Application.java | 5 ++--- .../com/techcourse/ManualHandlerMapping.java | 11 +++++------ .../techcourse/config/DataSourceConfig.java | 3 +-- .../techcourse/controller/LoginController.java | 10 +++++----- .../controller/RegisterController.java | 6 +++--- .../techcourse/controller/UserController.java | 10 +++++----- .../com/techcourse/controller/UserSession.java | 1 - .../main/java/com/techcourse/dao/UserDao.java | 5 ++--- .../repository/InMemoryUserRepository.java | 1 - .../jdbc/init/DataSourceConnectionManager.java | 7 +++---- .../jdbc/init/DatabasePopulatorUtils.java | 7 +++---- .../web/filter/CharacterEncodingFilter.java | 8 ++++++-- .../support/web/filter/ResourceFilter.java | 13 +++++++++---- .../webapp/assets/img/error-404-monochrome.svg | 2 +- .../java/com/techcourse/dao/UserDaoTest.java | 4 ++-- .../techcourse/service/UserServiceTest.java | 11 +++++------ .../jdbc/core/ConnectionManager.java | 3 +-- .../jdbc/core/JdbcTemplate.java | 7 +++---- .../jdbc/datasource/DataSourceUtils.java | 7 +++---- .../TransactionSynchronizationManager.java | 2 +- .../web/SpringServletContainerInitializer.java | 1 - .../web/bind/annotation/PathVariable.java | 6 +++++- .../web/bind/annotation/RequestParam.java | 6 +++++- .../org/springframework/web/servlet/View.java | 1 - .../web/servlet/mvc/DispatcherServlet.java | 2 +- .../web/servlet/mvc/HandlerAdapter.java | 3 +-- .../mvc/asis/ControllerHandlerAdapter.java | 2 +- .../servlet/mvc/asis/ForwardController.java | 1 - .../mvc/tobe/AnnotationHandlerMapping.java | 18 +++++++++++------- .../servlet/mvc/tobe/ControllerScanner.java | 7 +++---- .../web/servlet/mvc/tobe/HandlerExecution.java | 7 +++---- .../tobe/HandlerExecutionHandlerAdapter.java | 2 +- .../web/servlet/mvc/tobe/HandlerKey.java | 3 +-- .../web/servlet/view/JsonView.java | 5 ++--- .../web/servlet/view/JspView.java | 5 ++--- mvc/src/test/java/samples/TestController.java | 10 +++++----- .../mvc/tobe/AnnotationHandlerMappingTest.java | 8 ++++---- study/src/main/java/aop/Transactional.java | 7 ++++++- .../main/java/aop/config/DataSourceConfig.java | 3 +-- .../src/main/java/aop/repository/UserDao.java | 3 +-- .../java/connectionpool/DataSourceConfig.java | 3 +-- .../main/java/transaction/ConsumerWrapper.java | 3 +-- .../transaction/DatabasePopulatorUtils.java | 7 +++---- .../main/java/transaction/FunctionWrapper.java | 3 +-- study/src/test/java/aop/stage0/Stage0Test.java | 8 ++++---- study/src/test/java/aop/stage1/Stage1Test.java | 6 +++--- .../java/aop/stage1/TransactionPointcut.java | 3 +-- study/src/test/java/aop/stage2/Stage2Test.java | 6 +++--- .../connectionpool/PoolingVsNoPoolingTest.java | 11 +++++------ .../java/connectionpool/stage0/Stage0Test.java | 7 +++---- .../java/connectionpool/stage1/Stage1Test.java | 7 +++---- .../java/connectionpool/stage2/Stage2Test.java | 13 ++++++------- .../java/transaction/stage1/Stage1Test.java | 13 ++++++------- .../test/java/transaction/stage1/UserDao.java | 7 +++---- .../transaction/stage1/jdbc/JdbcTemplate.java | 7 +++---- .../transaction/stage2/FirstUserService.java | 11 +++++------ .../java/transaction/stage2/Stage2Test.java | 6 +++--- 58 files changed, 170 insertions(+), 180 deletions(-) diff --git a/app/src/main/java/com/techcourse/AppWebApplicationInitializer.java b/app/src/main/java/com/techcourse/AppWebApplicationInitializer.java index 024510e26c..6accfb3a19 100644 --- a/app/src/main/java/com/techcourse/AppWebApplicationInitializer.java +++ b/app/src/main/java/com/techcourse/AppWebApplicationInitializer.java @@ -1,13 +1,13 @@ package com.techcourse; import jakarta.servlet.ServletContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import web.org.springframework.web.WebApplicationInitializer; import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter; -import web.org.springframework.web.WebApplicationInitializer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class AppWebApplicationInitializer implements WebApplicationInitializer { diff --git a/app/src/main/java/com/techcourse/Application.java b/app/src/main/java/com/techcourse/Application.java index ee3e31f243..f28f75c675 100644 --- a/app/src/main/java/com/techcourse/Application.java +++ b/app/src/main/java/com/techcourse/Application.java @@ -1,13 +1,12 @@ package com.techcourse; +import java.io.File; +import java.util.stream.Stream; import org.apache.catalina.connector.Connector; import org.apache.catalina.startup.Tomcat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.util.stream.Stream; - public class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index 6119278f93..a25013cb93 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -1,15 +1,14 @@ package com.techcourse; -import com.techcourse.controller.*; +import com.techcourse.controller.LogoutController; import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.Map; public class ManualHandlerMapping implements HandlerMapping { diff --git a/app/src/main/java/com/techcourse/config/DataSourceConfig.java b/app/src/main/java/com/techcourse/config/DataSourceConfig.java index 9414a0599c..183b03b4ce 100644 --- a/app/src/main/java/com/techcourse/config/DataSourceConfig.java +++ b/app/src/main/java/com/techcourse/config/DataSourceConfig.java @@ -1,8 +1,7 @@ package com.techcourse.config; -import org.h2.jdbcx.JdbcDataSource; - import java.util.Objects; +import org.h2.jdbcx.JdbcDataSource; public class DataSourceConfig { diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 2f1d2d7e25..8932ee0749 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,15 +2,15 @@ import com.techcourse.domain.User; import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.view.JspView; -import webmvc.org.springframework.web.servlet.ModelAndView; -import context.org.springframework.stereotype.Controller; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; @Controller public class LoginController { diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 8c794c2d26..9bf80e171d 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,13 +2,13 @@ import com.techcourse.domain.User; import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.view.JspView; -import webmvc.org.springframework.web.servlet.ModelAndView; -import context.org.springframework.stereotype.Controller; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; @Controller public class RegisterController { diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java index d17bbcf0bb..9ab6171878 100644 --- a/app/src/main/java/com/techcourse/controller/UserController.java +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -1,15 +1,15 @@ package com.techcourse.controller; import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.view.JsonView; -import webmvc.org.springframework.web.servlet.ModelAndView; -import context.org.springframework.stereotype.Controller; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JsonView; @Controller public class UserController { diff --git a/app/src/main/java/com/techcourse/controller/UserSession.java b/app/src/main/java/com/techcourse/controller/UserSession.java index dc5fbbc556..e70c0c8313 100644 --- a/app/src/main/java/com/techcourse/controller/UserSession.java +++ b/app/src/main/java/com/techcourse/controller/UserSession.java @@ -2,7 +2,6 @@ import com.techcourse.domain.User; import jakarta.servlet.http.HttpSession; - import java.util.Optional; public class UserSession { diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 05b5c390ef..0624675948 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,11 +1,10 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; - import java.util.ArrayList; import java.util.List; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; public class UserDao { diff --git a/app/src/main/java/com/techcourse/repository/InMemoryUserRepository.java b/app/src/main/java/com/techcourse/repository/InMemoryUserRepository.java index 59cf0417e6..2692cf2c92 100644 --- a/app/src/main/java/com/techcourse/repository/InMemoryUserRepository.java +++ b/app/src/main/java/com/techcourse/repository/InMemoryUserRepository.java @@ -1,7 +1,6 @@ package com.techcourse.repository; import com.techcourse.domain.User; - import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; diff --git a/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java b/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java index ddcf77e1ad..bfffc10cc9 100644 --- a/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java +++ b/app/src/main/java/com/techcourse/support/jdbc/init/DataSourceConnectionManager.java @@ -1,12 +1,11 @@ package com.techcourse.support.jdbc.init; import com.techcourse.config.DataSourceConfig; -import org.springframework.jdbc.CannotGetJdbcConnectionException; -import org.springframework.jdbc.core.ConnectionManager; - -import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import javax.sql.DataSource; +import org.springframework.jdbc.CannotGetJdbcConnectionException; +import org.springframework.jdbc.core.ConnectionManager; public class DataSourceConnectionManager implements ConnectionManager { diff --git a/app/src/main/java/com/techcourse/support/jdbc/init/DatabasePopulatorUtils.java b/app/src/main/java/com/techcourse/support/jdbc/init/DatabasePopulatorUtils.java index 0a371ecfa3..e44d1b9c78 100644 --- a/app/src/main/java/com/techcourse/support/jdbc/init/DatabasePopulatorUtils.java +++ b/app/src/main/java/com/techcourse/support/jdbc/init/DatabasePopulatorUtils.java @@ -1,15 +1,14 @@ package com.techcourse.support.jdbc.init; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DatabasePopulatorUtils { diff --git a/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java b/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java index 0b87d7f99b..4dc7a307f1 100644 --- a/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java +++ b/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java @@ -1,8 +1,12 @@ package com.techcourse.support.web.filter; -import jakarta.servlet.*; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebFilter; - import java.io.IOException; @WebFilter("/*") diff --git a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java index 14de154b66..a66cbc27c0 100644 --- a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java +++ b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java @@ -1,15 +1,20 @@ package com.techcourse.support.web.filter; -import jakarta.servlet.*; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @WebFilter("/*") public class ResourceFilter implements Filter { diff --git a/app/src/main/webapp/assets/img/error-404-monochrome.svg b/app/src/main/webapp/assets/img/error-404-monochrome.svg index f0d345f991..e23c8713fc 100644 --- a/app/src/main/webapp/assets/img/error-404-monochrome.svg +++ b/app/src/main/webapp/assets/img/error-404-monochrome.svg @@ -1 +1 @@ -error-404-monochrome \ No newline at end of file +error-404-monochrome diff --git a/app/src/test/java/com/techcourse/dao/UserDaoTest.java b/app/src/test/java/com/techcourse/dao/UserDaoTest.java index 3a95053d8a..d7dee66a75 100644 --- a/app/src/test/java/com/techcourse/dao/UserDaoTest.java +++ b/app/src/test/java/com/techcourse/dao/UserDaoTest.java @@ -1,5 +1,7 @@ package com.techcourse.dao; +import static org.assertj.core.api.Assertions.assertThat; + import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.User; import com.techcourse.support.jdbc.init.DataSourceConnectionManager; @@ -8,8 +10,6 @@ import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; -import static org.assertj.core.api.Assertions.assertThat; - class UserDaoTest { private UserDao userDao; diff --git a/app/src/test/java/com/techcourse/service/UserServiceTest.java b/app/src/test/java/com/techcourse/service/UserServiceTest.java index c4ef89b995..bbbddecdc1 100644 --- a/app/src/test/java/com/techcourse/service/UserServiceTest.java +++ b/app/src/test/java/com/techcourse/service/UserServiceTest.java @@ -1,20 +1,19 @@ package com.techcourse.service; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + import com.techcourse.config.DataSourceConfig; import com.techcourse.dao.UserDao; import com.techcourse.dao.UserHistoryDao; import com.techcourse.domain.User; import com.techcourse.support.jdbc.init.DataSourceConnectionManager; import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.core.ConnectionManager; -import org.springframework.jdbc.core.JdbcTemplate; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; @Disabled class UserServiceTest { diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java b/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java index 8a8d0d45e6..91d122ef25 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/ConnectionManager.java @@ -1,8 +1,7 @@ package org.springframework.jdbc.core; -import org.springframework.jdbc.CannotGetJdbcConnectionException; - import java.sql.Connection; +import org.springframework.jdbc.CannotGetJdbcConnectionException; public interface ConnectionManager { 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 f80cf39170..1cdf08edb6 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,13 +1,12 @@ package org.springframework.jdbc.core; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.SqlQueryException; - import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.SqlQueryException; public class JdbcTemplate { diff --git a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index 3c40bfec52..95dfd283a5 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -1,11 +1,10 @@ package org.springframework.jdbc.datasource; -import org.springframework.jdbc.CannotGetJdbcConnectionException; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import javax.sql.DataSource; +import org.springframework.jdbc.CannotGetJdbcConnectionException; +import org.springframework.transaction.support.TransactionSynchronizationManager; // 4단계 미션에서 사용할 것 public abstract class DataSourceUtils { diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 715557fc66..bc32bddda5 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -1,8 +1,8 @@ package org.springframework.transaction.support; -import javax.sql.DataSource; import java.sql.Connection; import java.util.Map; +import javax.sql.DataSource; public abstract class TransactionSynchronizationManager { diff --git a/mvc/src/main/java/web/org/springframework/web/SpringServletContainerInitializer.java b/mvc/src/main/java/web/org/springframework/web/SpringServletContainerInitializer.java index b0e900ad6e..0c3efe6c95 100644 --- a/mvc/src/main/java/web/org/springframework/web/SpringServletContainerInitializer.java +++ b/mvc/src/main/java/web/org/springframework/web/SpringServletContainerInitializer.java @@ -5,7 +5,6 @@ import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.HandlesTypes; - import java.util.LinkedList; import java.util.List; import java.util.Set; diff --git a/mvc/src/main/java/web/org/springframework/web/bind/annotation/PathVariable.java b/mvc/src/main/java/web/org/springframework/web/bind/annotation/PathVariable.java index ceefd548e1..338da3ef35 100644 --- a/mvc/src/main/java/web/org/springframework/web/bind/annotation/PathVariable.java +++ b/mvc/src/main/java/web/org/springframework/web/bind/annotation/PathVariable.java @@ -1,6 +1,10 @@ package web.org.springframework.web.bind.annotation; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) diff --git a/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestParam.java b/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestParam.java index 5b3be4cedc..00e07bbb3f 100644 --- a/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestParam.java +++ b/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestParam.java @@ -1,6 +1,10 @@ package web.org.springframework.web.bind.annotation; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java index ec335db0a0..3158779892 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java @@ -2,7 +2,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; - import java.util.Map; public interface View { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java index c8d74cf60b..3b5df7753d 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java @@ -4,9 +4,9 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.servlet.ModelAndView; public class DispatcherServlet extends HttpServlet { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapter.java index 4475d5a206..95dd4871f6 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapter.java @@ -1,9 +1,8 @@ package webmvc.org.springframework.web.servlet.mvc; -import webmvc.org.springframework.web.servlet.ModelAndView; - import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; public interface HandlerAdapter { boolean supports(final Object handler); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java index 6c02851b91..0739dc7804 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java @@ -2,9 +2,9 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; import webmvc.org.springframework.web.servlet.view.JspView; -import webmvc.org.springframework.web.servlet.ModelAndView; public class ControllerHandlerAdapter implements HandlerAdapter { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java index cd8f1ef371..569076a804 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java @@ -2,7 +2,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; - import java.util.Objects; public class ForwardController implements Controller { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java index 5505b3eb2b..d45cc2cf7e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java @@ -1,16 +1,20 @@ package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; -import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import org.reflections.ReflectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; -import java.util.*; -import java.util.stream.Collectors; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; public class AnnotationHandlerMapping implements HandlerMapping { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ControllerScanner.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ControllerScanner.java index 4ced894a3d..fa559ab24c 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ControllerScanner.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ControllerScanner.java @@ -1,14 +1,13 @@ package webmvc.org.springframework.web.servlet.mvc.tobe; import context.org.springframework.stereotype.Controller; -import org.reflections.Reflections; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import java.util.Set; +import org.reflections.Reflections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ControllerScanner { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java index 22fa2dd98c..19561f66f6 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java @@ -2,12 +2,11 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.servlet.ModelAndView; public class HandlerExecution { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecutionHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecutionHandlerAdapter.java index 071cb6dcf0..3215e6ea2d 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecutionHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecutionHandlerAdapter.java @@ -2,8 +2,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; public class HandlerExecutionHandlerAdapter implements HandlerAdapter { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java index ce758bbdc1..298c33105f 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java @@ -1,8 +1,7 @@ package webmvc.org.springframework.web.servlet.mvc.tobe; -import web.org.springframework.web.bind.annotation.RequestMethod; - import java.util.Objects; +import web.org.springframework.web.bind.annotation.RequestMethod; public class HandlerKey { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java index 63c5684044..9b59396fb7 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java @@ -4,11 +4,10 @@ import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import web.org.springframework.http.MediaType; -import webmvc.org.springframework.web.servlet.View; - import java.io.IOException; import java.util.Map; +import web.org.springframework.http.MediaType; +import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index c61ee1846c..9aafe86efc 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -2,13 +2,12 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.Map; +import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.View; -import java.util.Map; -import java.util.Objects; - public class JspView implements View { private static final Logger log = LoggerFactory.getLogger(JspView.class); diff --git a/mvc/src/test/java/samples/TestController.java b/mvc/src/test/java/samples/TestController.java index fbed040d78..1f0e4acfb3 100644 --- a/mvc/src/test/java/samples/TestController.java +++ b/mvc/src/test/java/samples/TestController.java @@ -1,14 +1,14 @@ package samples; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.view.JspView; -import webmvc.org.springframework.web.servlet.ModelAndView; -import context.org.springframework.stereotype.Controller; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; @Controller public class TestController { diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index af8907e236..2dd9c5290c 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -1,14 +1,14 @@ package webmvc.org.springframework.web.servlet.mvc.tobe; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - class AnnotationHandlerMappingTest { private AnnotationHandlerMapping handlerMapping; diff --git a/study/src/main/java/aop/Transactional.java b/study/src/main/java/aop/Transactional.java index c9b91810c7..c54b914253 100644 --- a/study/src/main/java/aop/Transactional.java +++ b/study/src/main/java/aop/Transactional.java @@ -1,6 +1,11 @@ package aop; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) diff --git a/study/src/main/java/aop/config/DataSourceConfig.java b/study/src/main/java/aop/config/DataSourceConfig.java index b27245b932..5150597b27 100644 --- a/study/src/main/java/aop/config/DataSourceConfig.java +++ b/study/src/main/java/aop/config/DataSourceConfig.java @@ -1,12 +1,11 @@ package aop.config; +import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; -import javax.sql.DataSource; - @Configuration public class DataSourceConfig { diff --git a/study/src/main/java/aop/repository/UserDao.java b/study/src/main/java/aop/repository/UserDao.java index c9463f7323..0fe190462d 100644 --- a/study/src/main/java/aop/repository/UserDao.java +++ b/study/src/main/java/aop/repository/UserDao.java @@ -1,12 +1,11 @@ package aop.repository; import aop.domain.User; +import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; -import java.util.List; - @Repository public class UserDao { diff --git a/study/src/main/java/connectionpool/DataSourceConfig.java b/study/src/main/java/connectionpool/DataSourceConfig.java index 1d2c3e6b7d..23021a6585 100644 --- a/study/src/main/java/connectionpool/DataSourceConfig.java +++ b/study/src/main/java/connectionpool/DataSourceConfig.java @@ -2,11 +2,10 @@ import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import javax.sql.DataSource; - @Configuration public class DataSourceConfig { diff --git a/study/src/main/java/transaction/ConsumerWrapper.java b/study/src/main/java/transaction/ConsumerWrapper.java index be9d1b9678..970877a25d 100644 --- a/study/src/main/java/transaction/ConsumerWrapper.java +++ b/study/src/main/java/transaction/ConsumerWrapper.java @@ -1,10 +1,9 @@ package transaction; +import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.function.Consumer; - public final class ConsumerWrapper { private static final Logger log = LoggerFactory.getLogger(ConsumerWrapper.class); diff --git a/study/src/main/java/transaction/DatabasePopulatorUtils.java b/study/src/main/java/transaction/DatabasePopulatorUtils.java index 84195470d9..a2cda92b34 100644 --- a/study/src/main/java/transaction/DatabasePopulatorUtils.java +++ b/study/src/main/java/transaction/DatabasePopulatorUtils.java @@ -1,15 +1,14 @@ package transaction; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DatabasePopulatorUtils { diff --git a/study/src/main/java/transaction/FunctionWrapper.java b/study/src/main/java/transaction/FunctionWrapper.java index 7e1e35fff7..b6dfe4521a 100644 --- a/study/src/main/java/transaction/FunctionWrapper.java +++ b/study/src/main/java/transaction/FunctionWrapper.java @@ -1,10 +1,9 @@ package transaction; +import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.function.Function; - public class FunctionWrapper { private static final Logger log = LoggerFactory.getLogger(FunctionWrapper.class); diff --git a/study/src/test/java/aop/stage0/Stage0Test.java b/study/src/test/java/aop/stage0/Stage0Test.java index 079cc6b5a0..c55242e743 100644 --- a/study/src/test/java/aop/stage0/Stage0Test.java +++ b/study/src/test/java/aop/stage0/Stage0Test.java @@ -1,5 +1,9 @@ package aop.stage0; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; + import aop.DataAccessException; import aop.StubUserHistoryDao; import aop.domain.User; @@ -15,10 +19,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.PlatformTransactionManager; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; - @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class Stage0Test { diff --git a/study/src/test/java/aop/stage1/Stage1Test.java b/study/src/test/java/aop/stage1/Stage1Test.java index 113b2e7d03..3bec0629c0 100644 --- a/study/src/test/java/aop/stage1/Stage1Test.java +++ b/study/src/test/java/aop/stage1/Stage1Test.java @@ -1,5 +1,8 @@ package aop.stage1; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + import aop.DataAccessException; import aop.StubUserHistoryDao; import aop.domain.User; @@ -13,9 +16,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.PlatformTransactionManager; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class Stage1Test { diff --git a/study/src/test/java/aop/stage1/TransactionPointcut.java b/study/src/test/java/aop/stage1/TransactionPointcut.java index 29ff854890..e8d7a4b3d0 100644 --- a/study/src/test/java/aop/stage1/TransactionPointcut.java +++ b/study/src/test/java/aop/stage1/TransactionPointcut.java @@ -1,8 +1,7 @@ package aop.stage1; -import org.springframework.aop.support.StaticMethodMatcherPointcut; - import java.lang.reflect.Method; +import org.springframework.aop.support.StaticMethodMatcherPointcut; /** * 포인트컷(pointcut). 어드바이스를 적용할 조인 포인트를 선별하는 클래스. diff --git a/study/src/test/java/aop/stage2/Stage2Test.java b/study/src/test/java/aop/stage2/Stage2Test.java index ae46641880..7f2e229ace 100644 --- a/study/src/test/java/aop/stage2/Stage2Test.java +++ b/study/src/test/java/aop/stage2/Stage2Test.java @@ -1,5 +1,8 @@ package aop.stage2; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + import aop.DataAccessException; import aop.StubUserHistoryDao; import aop.domain.User; @@ -10,9 +13,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class Stage2Test { diff --git a/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java b/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java index 9025e36b9f..ec7206f944 100644 --- a/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java +++ b/study/src/test/java/connectionpool/PoolingVsNoPoolingTest.java @@ -4,6 +4,11 @@ import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.util.ClockSource; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import javax.sql.DataSource; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -12,12 +17,6 @@ import org.testcontainers.containers.MySQLContainer; import org.testcontainers.utility.DockerImageName; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - /** * pooling을 사용한 경우와 사용하지 않은 경우 트래픽이 얼마나 차이나는지 확인해보자. * diff --git a/study/src/test/java/connectionpool/stage0/Stage0Test.java b/study/src/test/java/connectionpool/stage0/Stage0Test.java index 7eb3d7d87d..94421bb3e8 100644 --- a/study/src/test/java/connectionpool/stage0/Stage0Test.java +++ b/study/src/test/java/connectionpool/stage0/Stage0Test.java @@ -1,13 +1,12 @@ package connectionpool.stage0; -import org.h2.jdbcx.JdbcDataSource; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; - -import static org.assertj.core.api.Assertions.assertThat; +import org.h2.jdbcx.JdbcDataSource; +import org.junit.jupiter.api.Test; class Stage0Test { diff --git a/study/src/test/java/connectionpool/stage1/Stage1Test.java b/study/src/test/java/connectionpool/stage1/Stage1Test.java index 087485ff58..062caf42e1 100644 --- a/study/src/test/java/connectionpool/stage1/Stage1Test.java +++ b/study/src/test/java/connectionpool/stage1/Stage1Test.java @@ -1,14 +1,13 @@ package connectionpool.stage1; +import static org.assertj.core.api.Assertions.assertThat; + import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import java.sql.SQLException; import org.h2.jdbcx.JdbcConnectionPool; import org.junit.jupiter.api.Test; -import java.sql.SQLException; - -import static org.assertj.core.api.Assertions.assertThat; - class Stage1Test { private static final String H2_URL = "jdbc:h2:./test;DB_CLOSE_DELAY=-1"; diff --git a/study/src/test/java/connectionpool/stage2/Stage2Test.java b/study/src/test/java/connectionpool/stage2/Stage2Test.java index 2ed6b075b5..a922239f1b 100644 --- a/study/src/test/java/connectionpool/stage2/Stage2Test.java +++ b/study/src/test/java/connectionpool/stage2/Stage2Test.java @@ -1,21 +1,20 @@ package connectionpool.stage2; +import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; +import static org.assertj.core.api.Assertions.assertThat; + import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.pool.HikariPool; import connectionpool.DataSourceConfig; +import java.lang.reflect.Field; +import java.sql.Connection; +import javax.sql.DataSource; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import javax.sql.DataSource; -import java.lang.reflect.Field; -import java.sql.Connection; - -import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; -import static org.assertj.core.api.Assertions.assertThat; - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class Stage2Test { diff --git a/study/src/test/java/transaction/stage1/Stage1Test.java b/study/src/test/java/transaction/stage1/Stage1Test.java index 8c29944d4e..acd7c50cda 100644 --- a/study/src/test/java/transaction/stage1/Stage1Test.java +++ b/study/src/test/java/transaction/stage1/Stage1Test.java @@ -1,7 +1,13 @@ package transaction.stage1; +import static org.assertj.core.api.Assertions.assertThat; + import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.concurrent.TimeUnit; +import javax.sql.DataSource; import org.h2.jdbcx.JdbcDataSource; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -13,13 +19,6 @@ import transaction.DatabasePopulatorUtils; import transaction.RunnableWrapper; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; - /** * 격리 레벨(Isolation Level)에 따라 여러 사용자가 동시에 db에 접근했을 때 어떤 문제가 발생하는지 확인해보자. * ❗phantom reads는 docker를 실행한 상태에서 테스트를 실행한다. diff --git a/study/src/test/java/transaction/stage1/UserDao.java b/study/src/test/java/transaction/stage1/UserDao.java index dad7915cba..fe1f287f20 100644 --- a/study/src/test/java/transaction/stage1/UserDao.java +++ b/study/src/test/java/transaction/stage1/UserDao.java @@ -1,11 +1,10 @@ package transaction.stage1; -import transaction.stage1.jdbc.JdbcTemplate; -import transaction.stage1.jdbc.RowMapper; - -import javax.sql.DataSource; import java.sql.Connection; import java.util.List; +import javax.sql.DataSource; +import transaction.stage1.jdbc.JdbcTemplate; +import transaction.stage1.jdbc.RowMapper; public class UserDao { diff --git a/study/src/test/java/transaction/stage1/jdbc/JdbcTemplate.java b/study/src/test/java/transaction/stage1/jdbc/JdbcTemplate.java index 9d64be5a99..805579f4ed 100644 --- a/study/src/test/java/transaction/stage1/jdbc/JdbcTemplate.java +++ b/study/src/test/java/transaction/stage1/jdbc/JdbcTemplate.java @@ -1,14 +1,13 @@ package transaction.stage1.jdbc; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class JdbcTemplate { private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); diff --git a/study/src/test/java/transaction/stage2/FirstUserService.java b/study/src/test/java/transaction/stage2/FirstUserService.java index 9a1d415c18..f662487454 100644 --- a/study/src/test/java/transaction/stage2/FirstUserService.java +++ b/study/src/test/java/transaction/stage2/FirstUserService.java @@ -1,5 +1,10 @@ package transaction.stage2; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -8,12 +13,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionSynchronizationManager; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; - @Service public class FirstUserService { diff --git a/study/src/test/java/transaction/stage2/Stage2Test.java b/study/src/test/java/transaction/stage2/Stage2Test.java index e522bf4365..3e9c95ab41 100644 --- a/study/src/test/java/transaction/stage2/Stage2Test.java +++ b/study/src/test/java/transaction/stage2/Stage2Test.java @@ -1,5 +1,8 @@ package transaction.stage2; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -7,9 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - /** * 트랜잭션 전파(Transaction Propagation)란? * 트랜잭션의 경계에서 이미 진행 중인 트랜잭션이 있을 때 또는 없을 때 어떻게 동작할 것인가를 결정하는 방식을 말한다. From 930c3e0d80d01d39f8ec806a54543cee3dfdf285 Mon Sep 17 00:00:00 2001 From: yoondgu Date: Fri, 29 Sep 2023 06:03:31 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EC=A1=B0=ED=9A=8C=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EB=8B=A8=EA=B1=B4=20=EC=A1=B0=ED=9A=8C,=20?= =?UTF-8?q?=EB=8B=A4=EA=B1=B4=20=EC=A1=B0=ED=9A=8C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 28 ++++--------------- .../jdbc/core/JdbcTemplate.java | 24 ++++++++++++++-- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 0624675948..81f5db453b 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,37 +1,19 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class UserDao { - private static final RowMapper USER_ROW_MAPPER = resultSet -> { - if (resultSet.next()) { - return new User( + private static final RowMapper USER_ROW_MAPPER = resultSet -> + new User( resultSet.getLong(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4) ); - } - return null; - }; - private static final RowMapper> USERS_ROW_MAPPER = resultSet -> { - List users = new ArrayList<>(); - while (resultSet.next()) { - users.add(new User( - resultSet.getLong(1), - resultSet.getString(2), - resultSet.getString(3), - resultSet.getString(4) - )); - } - return users; - }; - private final JdbcTemplate jdbcTemplate; public UserDao(JdbcTemplate jdbcTemplate) { @@ -59,16 +41,16 @@ public void update(final User user) { public List findAll() { final var sql = "select id, account, password, email from users"; - return jdbcTemplate.executeQuery(sql, USERS_ROW_MAPPER); + return jdbcTemplate.executeQueryForList(sql, USER_ROW_MAPPER); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; - return jdbcTemplate.executeQuery(sql, USER_ROW_MAPPER, id); + return jdbcTemplate.executeQueryForObject(sql, USER_ROW_MAPPER, id); } public User findByAccount(final String account) { final var sql = "select id, account, password, email from users where account = ?"; - return jdbcTemplate.executeQuery(sql, USER_ROW_MAPPER, account); + return jdbcTemplate.executeQueryForObject(sql, USER_ROW_MAPPER, account); } } 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 1cdf08edb6..4db10da224 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -4,6 +4,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.SqlQueryException; @@ -29,12 +31,30 @@ public void executeUpdate(String query, Object... parameters) { } } - public T executeQuery(String query, RowMapper rowMapper, Object... parameters) { + public T executeQueryForObject(String query, RowMapper rowMapper, Object... parameters) { try (final Connection connection = connectionManager.getConnection(); final PreparedStatement preparedStatement = connection.prepareStatement(query); final ResultSet resultSet = executePreparedStatementQuery(preparedStatement, parameters)) { log.info("query: {}", query); - return rowMapper.mapRow(resultSet); + if (resultSet.next()) { + return rowMapper.mapRow(resultSet); + } + return null; + } catch (SQLException exception) { + throw new SqlQueryException(exception.getMessage(), query); + } + } + + public List executeQueryForList(String query, RowMapper rowMapper, Object... parameters) { + try (final Connection connection = connectionManager.getConnection(); + final PreparedStatement preparedStatement = connection.prepareStatement(query); + final ResultSet resultSet = executePreparedStatementQuery(preparedStatement, parameters)) { + log.info("query: {}", query); + final List results = new ArrayList<>(); + while (resultSet.next()) { + results.add(rowMapper.mapRow(resultSet)); + } + return results; } catch (SQLException exception) { throw new SqlQueryException(exception.getMessage(), query); }