|
| 1 | +package com.example.solidconnection.common.config.datasource; |
| 2 | + |
| 3 | +import com.example.solidconnection.common.listener.QueryMetricsListener; |
| 4 | +import com.zaxxer.hikari.HikariDataSource; |
| 5 | +import javax.sql.DataSource; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.boot.autoconfigure.flyway.FlywayDataSource; |
| 10 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.context.annotation.Primary; |
| 14 | + |
| 15 | +@RequiredArgsConstructor |
| 16 | +@Configuration |
| 17 | +public class DataSourceConfig { |
| 18 | + |
| 19 | + private final QueryMetricsListener queryMetricsListener; |
| 20 | + |
| 21 | + @Bean |
| 22 | + @Primary |
| 23 | + public DataSource proxyDataSource(DataSourceProperties props) { |
| 24 | + DataSource dataSource = props.initializeDataSourceBuilder().build(); |
| 25 | + |
| 26 | + return ProxyDataSourceBuilder |
| 27 | + .create(dataSource) |
| 28 | + .listener(queryMetricsListener) |
| 29 | + .name("main") |
| 30 | + .build(); |
| 31 | + } |
| 32 | + |
| 33 | + // Flyway 전용 DataSource (Proxy 미적용) |
| 34 | + @Bean |
| 35 | + @FlywayDataSource |
| 36 | + public DataSource flywayDataSource( |
| 37 | + @Value("${spring.datasource.url}") String url, |
| 38 | + @Value("${spring.flyway.user}") String username, |
| 39 | + @Value("${spring.flyway.password}") String password, |
| 40 | + @Value("${spring.datasource.driverClassName}") String driverClassName |
| 41 | + ) { |
| 42 | + HikariDataSource dataSource = new HikariDataSource(); |
| 43 | + dataSource.setJdbcUrl(url); |
| 44 | + dataSource.setUsername(username); |
| 45 | + dataSource.setPassword(password); |
| 46 | + dataSource.setDriverClassName(driverClassName); |
| 47 | + dataSource.setPoolName("FlywayPool"); |
| 48 | + |
| 49 | + dataSource.setMinimumIdle(0); |
| 50 | + dataSource.setMaximumPoolSize(2); |
| 51 | + dataSource.setConnectionTimeout(10000); |
| 52 | + dataSource.setIdleTimeout(60000); // 1분으로 단축 |
| 53 | + dataSource.setMaxLifetime(300000); // 최대 5분 |
| 54 | + |
| 55 | + return dataSource; |
| 56 | + } |
| 57 | +} |
0 commit comments