본문 바로가기
Dev/Spring Boot

[스프링 부트 개념과 활용] 스프링 데이터 3, 4부

by dev_jsk 2020. 8. 27.
728x90
반응형

지원하는 DBCP

DBCP

Connection Pool이라는 공간을 만들어 Connection객체들을 담아놓고 차후 사용자가 DataBase에 접속을 시도하면 Connection Pool에 담겨있는 Connection 객체를 하나하나 꺼내주는 방법

 

종류

  • HikariCP
  • Tomcat CP
  • Commons DBCP2

DBCP 설정 방법

  • spring.datasource.hikari.*
  • spring.datasource.tomcat.*
  • spring.datasource.dbcp2.*

참고

https://github.com/brettwooldridge/HikariCP#frequently-used

 

brettwooldridge/HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last. - brettwooldridge/HikariCP

github.com

MySQL

예제

// pom.xml
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
// application.properties
// DB 접속정보 설정
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=jinseo
spring.datasource.password=pass
// MySQLRunner.java

@Component
public class MySQLRunner implements ApplicationRunner {

    @Autowired
    DataSource dataSource;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try(Connection connection = dataSource.getConnection()) {
            String url = connection.getMetaData().getURL();
            String userName = connection.getMetaData().getUserName();
    
            System.out.println("URL : " + url);
            System.out.println("UserName : " + userName);
    
            Statement statement = connection.createStatement();
            String sql = "CREATE TABLE USER(ID INTEGER NOT NULL, NAME VARCHAR(255), PRIMARY KEY (ID))";
            statement.executeUpdate(sql);
        }

        jdbcTemplate.execute("INSERT INTO USER VALUES (1, 'Jinseo')");
    }
}

Docker를 통해 MySQL 서버를 구성하고 H2이용한 테스트와 동일하게 실행.

도커 (Docker)

 

Docker overview

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you...

docs.docker.com

PostgreSQL

// pom.xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>
// application.properties
// DB 접속정보 설정
spring.datasource.url=jdbc:postgresql://localhost:5432/springboot
spring.datasource.username=jinseo
spring.datasource.password=pass

Docker를 통해 PostgreSQL 서버를 구성하고 H2이용한 테스트와 동일하게 실행.

728x90
반응형

댓글