|
| 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 | + // Driver |
| 22 | + public static final String FLYWAY_MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver"; |
| 23 | + |
| 24 | + // Pool Name |
| 25 | + public static final String FLYWAY_POOL_NAME = "FlywayPool"; |
| 26 | + |
| 27 | + // Connection Pool Settings |
| 28 | + public static final int FLYWAY_MINIMUM_IDLE = 0; // 유휴 커넥션을 0으로 설정하면 사용하지 않을 때 커넥션을 즉시 반납 |
| 29 | + public static final int FLYWAY_MAXIMUM_POOL_SIZE = 2; |
| 30 | + public static final long FLYWAY_CONNECTION_TIMEOUT = 10000L; |
| 31 | + public static final long FLYWAY_IDLE_TIMEOUT = 60000L; // 1분 |
| 32 | + public static final long FLYWAY_MAX_LIFETIME = 300000L; // 5분 |
| 33 | + |
| 34 | + @Bean |
| 35 | + @Primary |
| 36 | + public DataSource proxyDataSource(DataSourceProperties props) { |
| 37 | + DataSource dataSource = props.initializeDataSourceBuilder().build(); |
| 38 | + |
| 39 | + return ProxyDataSourceBuilder |
| 40 | + .create(dataSource) |
| 41 | + .listener(queryMetricsListener) |
| 42 | + .name("main") |
| 43 | + .build(); |
| 44 | + } |
| 45 | + |
| 46 | + // Flyway 전용 DataSource (Proxy 미적용) |
| 47 | + @Bean |
| 48 | + @FlywayDataSource |
| 49 | + public DataSource flywayDataSource( |
| 50 | + @Value("${spring.datasource.url}") String url, |
| 51 | + @Value("${spring.flyway.user:${spring.datasource.username}}") String username, |
| 52 | + @Value("${spring.flyway.password:${spring.datasource.password}}") String password |
| 53 | + ) { |
| 54 | + HikariDataSource dataSource = new HikariDataSource(); |
| 55 | + dataSource.setJdbcUrl(url); |
| 56 | + dataSource.setUsername(username); |
| 57 | + dataSource.setPassword(password); |
| 58 | + dataSource.setDriverClassName(FLYWAY_MYSQL_DRIVER); |
| 59 | + dataSource.setPoolName(FLYWAY_POOL_NAME); |
| 60 | + |
| 61 | + dataSource.setMinimumIdle(FLYWAY_MINIMUM_IDLE); |
| 62 | + dataSource.setMaximumPoolSize(FLYWAY_MAXIMUM_POOL_SIZE); |
| 63 | + dataSource.setConnectionTimeout(FLYWAY_CONNECTION_TIMEOUT); |
| 64 | + dataSource.setIdleTimeout(FLYWAY_IDLE_TIMEOUT); // 1분으로 단축 |
| 65 | + dataSource.setMaxLifetime(FLYWAY_MAX_LIFETIME); // 최대 5분 |
| 66 | + |
| 67 | + return dataSource; |
| 68 | + } |
| 69 | +} |
0 commit comments