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&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
max-lifetime: 60000
connection-timeout: 30000
validation-timeout: 5000
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
jpa:
show-sql: false
hibernate:
ddl-auto: 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")
public LocalContainerEntityManagerFactoryBean andongEntityManager() {
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")
public PlatformTransactionManager dbMallTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(andongEntityManager().getObject());
return transactionManager;
}
@Bean(name = "hikari")
public HikariDataSource 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;
}
}