헤르메스 LIFE

[Spring Boot] HikariCP를 이용한 Database Connection + JPA 본문

Spring Boot Framework

[Spring Boot] HikariCP를 이용한 Database Connection + JPA

헤르메스의날개 2022. 2. 11. 01:06
728x90

개발환경

1. STS 버전 : 4.13.1

2. JDK 버전 : OpenJDK 11.0.14_9_x64

3. Tomcat 버전 : 9.0.56

4. Maven 버전 : 3.8.4

5. Spring 버전 : Spring Boot 2.6.3

6. Database : Docker 에 DB 설치

- primary - PostgreSQL 13.3

8. lombok


PostgreSQL 연결하기 - Docker 를 이용한 PostgreSQL 설치

https://hermeslog.tistory.com/541

 

[SpringBoot] PostgreSQL 연결하기

IDE : IntelliJ JDK : OpenJDK 11 Framework : Spring Boot 2.5.2 Database : PostgreSQL 최신버전 ( 라이센스도 소스공개의무도 없음 ) 첨부파일이 MySqlRunner 로 되어있는데.. MySQL 접속테스트 중 소스만 바..

hermeslog.tistory.com

 

PostgreSQL 접속하기 - Docker 를 이용한 PostgreSQL 연결

# Docker에 설치된 컨테이너 목록 조회
> docker ps -a

# Docker 컨테이너 실행
> docker exec -it postgres_boot bash

# postgres 사용자 접속
> su - postgres

# postgres 계정접속
> psql --username hermeswing --dbname springboot

# 테이블 목록 조회
> \dt
          List of relations
 Schema |  Name  | Type  |   Owner
--------+--------+-------+------------
 public | mybook | table | hermeswing
 public | test   | table | hermeswing

 


1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2. application.xml

server:
  port: 9090
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true
      force: true
      
spring:
    
  h2:
    console:
      enabled: true
      path: /h2-console

  datasource:
      url: jdbc:postgresql://localhost:5432/springboot
      username: hermeswing
      password: pass
      hikari:
        idle-timeout: 10000
        maximum-pool-size: 10
        minimum-idle: 5
        pool-name: BaseHikariPool
              
  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: update         # Hibernate ddl auto (create, create-drop, validate, update)
    show-sql: false
    properties:
      hibernatte:
        format_sql: true
  profiles:
    active: local


logging:
  level:
    org:
      hibernate:
        SQL: debug
        type:
          descriptor:
            SQL: trace

 

DatasourceConfig.java

package com.study.springboot.config;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(basePackages = { "com.study.springboot.system" },    // Repository 경로
        entityManagerFactoryRef = "baseEntityManager", transactionManagerRef = "baseTransactionManager")
public class DatasourceConfig {
    @Autowired
    Environment env;

    @Bean
    // @ConfigurationProperties(prefix = "spring.datasource") // default :
    // spring.datasource
    public DataSourceProperties baseDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource primeDataSource() {
        return baseDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean baseEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(primeDataSource());

        // Entiry Package 경로
        em.setPackagesToScan(new String[] { "com.study.springboot.system.entity" });
        em.setPersistenceUnitName("baseEntityManager");                               // 영속성 객체 이름을 지정

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        // Hibernate 설정
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    public PlatformTransactionManager baseTransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(baseEntityManager().getObject());
        return transactionManager;
    }

}

Member.java

package com.study.springboot.system.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity @NoArgsConstructor
@Getter
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long   id;
    private String username;
    private String password;

    /**
     * @param username
     * @param password
     */
    public Member(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

MemberRepository.java

package com.study.springboot.system.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.study.springboot.system.entity.Member;

@Repository
public interface MemberRepository extends CrudRepository<Member, Long> {
}

MemberRepositoryTest.java

package com.study.springboot.system.repository;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.study.springboot.system.entity.Member;

@RunWith(SpringRunner.class)
// @DataJpaTest // @Transactional 포함. 테스트가 완료되면 자동으로 롤백.
@SpringBootTest
public class MemberRepositoryTest {

    @Autowired(required = true)
    MemberRepository memberRepository;

    @Test
    // @Transactional // Default primary 를 Call 합니다.
    public void testMember() throws Exception {
        // Given
        Member memberA = new Member("memberA", "");

        // When
        Member member     = memberRepository.save(memberA);
        Member findMember = memberRepository.findById(member.getId()).get();

        // Then
        Assertions.assertThat(findMember.getId()).isEqualTo(memberA.getId());
        Assertions.assertThat(findMember.getUsername()).isEqualTo(memberA.getUsername());
    }
}

결과

springboot=# select *
 from member;
  id  | password | username
------+----------+----------
 2205 |          | memberA
(1 row)

 

참고

[Major DB Dialect]
Oracle(any) : org.hibernate.dialect.OracleDialect
Oracle10g : org.hibernate.dialect.Oracle10gDialect
MySql, MariaDB : org.hibernate.dialect.MySQL5Dialect
MsSql2000 : org.hibernate.dialect.SQLServerDialect 
MsSql2005 : org.hibernate.dialect.SQLServer2005Dialect
MsSql2008 : org.hibernate.dialect.SQLServer2008Dialect

[hbm2ddl.auto options]
update : 객체와 스키마 비교, 기존 스키마 유지, 추가/삭제만 진행
create : 시작 시 스키마 삭제, 새로 생성
create-drop : SessionFactory 종료 시 스키마 삭제

 

https://www.bezkoder.com/spring-boot-postgresql-example/

 

728x90