SpringBoot Mysql Config

민지킴·2021년 6월 2일
0
post-custom-banner

application.yml

---
spring:
  profiles:
    active:
    - dblocal
    - redislocal
    - tomcatlocal
    - elasticlocal
    - kafkaLocal
    - dbDev
    - loglocal

spring:
  profiles: dbDev
  andong:
    datasource:
      #url: jdbc:mysql://localhost:3306/studydb?&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false #쓰기 DB
      url: jdbc:mysql://localhost:3306/studydb?&autoReconnect=true&serverTimezone=UTC&characterEncoding=UTF-8
      username: root  
      password: 123456
      driverClassName: com.mysql.cj.jdbc.Driver
      type: com.zaxxer.hikari.HikariDataSource
      dbcp2:
        validation-query: SELECT 1
      hikari:      
        maximum-pool-size: 10
        #minimum-idle: 100
        max-lifetime: 60000 #1800000
        connection-timeout: 30000 #30000
        validation-timeout: 5000
        #connection-test-query: SELECT 1      
        data-source-properties: 
          cachePrepStmts: true
          prepStmtCacheSize: 250
          prepStmtCacheSqlLimit: 2048
          useServerPrepStmts: true
          
  jpa:  
    show-sql: false
    #database-platform: org.hibernate.dialect.MySQL5Dialect    
    hibernate:    
      ddl-auto: none #create 로 해놓으면 Data전체 삭제 됨. 개발 초기에는 update 사용하나 상용에는 none 필수.
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
---

DB Config

package a.b.c.d.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
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.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@EnableJpaRepositories(basePackages = "a.b.c.d.repository", entityManagerFactoryRef = "andongEntityManager", transactionManagerRef = "andongTransactionManager")
public class DBConfig {
	@Autowired
	private Environment env;

	private static final String prefix = "spring.andong.datasource.";

	@Bean(name = "andongEntityManager")
	// @Primary
	public LocalContainerEntityManagerFactoryBean andongEntityManager() {
		// log.info("LocalContainerEntityManagerFactoryBean");
		LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
		em.setDataSource(dbMallDataSource());
		em.setPackagesToScan(new String[] { "co.kr.smartplusteam.luna.study.vo" });

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

		Map<String, Object> jpaProperties = new HashMap<>();
		em.setJpaPropertyMap(jpaProperties);
		return em;
	}

	@Bean(name = "andongTransactionManager")
	// @Primary
	public PlatformTransactionManager dbMallTransactionManager() {
		// log.info("dbMallTransactionManager");

		JpaTransactionManager transactionManager = new JpaTransactionManager();
		transactionManager.setEntityManagerFactory(andongEntityManager().getObject());

		return transactionManager;
	}

	@Bean(name = "hikari")
	// @Bean
	// @Primary
	public HikariDataSource dbMallDataSource() {
		// log.info("dbMallDataSource");

		HikariConfig config = new HikariConfig();

		config.setJdbcUrl(env.getProperty(prefix + "url"));
		config.setUsername(env.getProperty(prefix + "username"));
		config.setPassword(env.getProperty(prefix + "password"));
		config.setMaxLifetime(Long.parseLong(env.getProperty(prefix + "hikari.max-lifetime")));
		config.setConnectionTimeout(Long.parseLong(env.getProperty(prefix + "hikari.connection-timeout")));
		config.setValidationTimeout(Long.parseLong(env.getProperty(prefix + "hikari.validation-timeout")));
		config.addDataSourceProperty("cachePrepStmts", env.getProperty(prefix + "hikari.data-source-properties.cachePrepStmts"));
		config.addDataSourceProperty("prepStmtCacheSize", env.getProperty(prefix + "hikari.data-source-properties.prepStmtCacheSize"));
		config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getProperty(prefix + "hikari.data-source-properties.prepStmtCacheSqlLimit"));
		config.addDataSourceProperty("useServerPrepStmts", env.getProperty(prefix + "hikari.data-source-properties.useServerPrepStmts"));

		HikariDataSource dataSource = new HikariDataSource(config);

		return dataSource;
	}
}
profile
하루하루는 성실하게 인생 전체는 되는대로
post-custom-banner

0개의 댓글