新增:数据源预热回退

This commit is contained in:
yangyang01000846
2026-01-23 18:43:17 +08:00
parent 06301880fe
commit 606c445cb8

View File

@@ -1,47 +1,47 @@
package com.sdm.common.config;//package com.sdm.project.config.mybatis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.Connection;
@Slf4j
@Configuration
public class DataSourcePreWarmer {
@Bean
public ApplicationRunner secondDataSourcePreWarmer(
@Qualifier("secondDataSource") DataSource secondDataSource) {
return args -> {
try (Connection conn = secondDataSource.getConnection()) {
log.info("✅ secondDataSource 预热成功,连接已建立: {}", conn);
} catch (Exception e) {
log.error("❌ secondDataSource 预热失败", e);
throw new RuntimeException(e);
}
};
}
@Bean
public ApplicationRunner mainDataSourcePreWarmer(
@Qualifier("masterDataSource") DataSource master,
@Qualifier("slaveDataSource") DataSource slave) {
return args -> {
try {
try (Connection c1 = master.getConnection()) {
log.info("✅ masterDataSource 预热成功: {}", c1);
}
try (Connection c2 = slave.getConnection()) {
log.info("✅ slaveDataSource 预热成功: {}", c2);
}
} catch (Exception e) {
log.error("❌ 主从数据源预热失败", e);
throw new RuntimeException(e);
}
};
}
}
//package com.sdm.common.config;//package com.sdm.project.config.mybatis;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.ApplicationRunner;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.sql.DataSource;
//import java.sql.Connection;
//
//@Slf4j
//@Configuration
//public class DataSourcePreWarmer {
//
// @Bean
// public ApplicationRunner secondDataSourcePreWarmer(
// @Qualifier("secondDataSource") DataSource secondDataSource) {
// return args -> {
// try (Connection conn = secondDataSource.getConnection()) {
// log.info("✅ secondDataSource 预热成功,连接已建立: {}", conn);
// } catch (Exception e) {
// log.error("❌ secondDataSource 预热失败", e);
// throw new RuntimeException(e);
// }
// };
// }
//
// @Bean
// public ApplicationRunner mainDataSourcePreWarmer(
// @Qualifier("masterDataSource") DataSource master,
// @Qualifier("slaveDataSource") DataSource slave) {
// return args -> {
// try {
// try (Connection c1 = master.getConnection()) {
// log.info("✅ masterDataSource 预热成功: {}", c1);
// }
// try (Connection c2 = slave.getConnection()) {
// log.info("✅ slaveDataSource 预热成功: {}", c2);
// }
// } catch (Exception e) {
// log.error("❌ 主从数据源预热失败", e);
// throw new RuntimeException(e);
// }
// };
// }
//}