11package com .example .solidconnection .common .config .datasource ;
22
33import com .example .solidconnection .common .listener .QueryMetricsListener ;
4+ import com .zaxxer .hikari .HikariDataSource ;
45import javax .sql .DataSource ;
56import lombok .RequiredArgsConstructor ;
67import net .ttddyy .dsproxy .support .ProxyDataSourceBuilder ;
8+ import org .springframework .beans .factory .annotation .Value ;
9+ import org .springframework .boot .autoconfigure .flyway .FlywayDataSource ;
710import org .springframework .boot .autoconfigure .jdbc .DataSourceProperties ;
811import org .springframework .context .annotation .Bean ;
912import org .springframework .context .annotation .Configuration ;
@@ -15,6 +18,19 @@ public class DataSourceProxyConfig {
1518
1619 private final QueryMetricsListener queryMetricsListener ;
1720
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+
1834 @ Bean
1935 @ Primary
2036 public DataSource proxyDataSource (DataSourceProperties props ) {
@@ -26,4 +42,28 @@ public DataSource proxyDataSource(DataSourceProperties props) {
2642 .name ("main" )
2743 .build ();
2844 }
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+ }
2969}
0 commit comments