如何在springboot中捕获hikaripool初始化期间发生的异常?

zkure5ic  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(356)

我的数据库有有限的活动连接,这会导致hikaripool初始化异常,如下所示,我想忽略整个stacktrace并在我的主类中捕获异常。
以下是我的异常日志的堆栈跟踪:

2020-11-18 16:27:16.619  INFO 9124 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-11-18 16:27:18.344 ERROR 9124 --- [  restartedMain] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

java.sql.SQLSyntaxErrorException: User 6eX6BxR3TY already has more than 'max_user_connections' active connections
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)

主要课程如下:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class NseapiApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(NseapiApplication.class);

    private static String splunkUrl;

    @Value("${splunk.url}")
    public void setSplunkUrl(String splunkUrl) {
        NseapiApplication.splunkUrl = splunkUrl;
    }

    public static void main(String[] args) {
        SpringApplication.run(NseapiApplication.class, args);
        LOGGER.info("Forwarding logs to Splunk Cloud Instance : " + splunkUrl);

    }

    @Bean
    public static BeanFactoryPostProcessor dependsOnPostProcessor() {
        return bf -> {
            String[] jpa = bf.getBeanNamesForType(EntityManagerFactory.class);
            Stream.of(jpa).map(bf::getBeanDefinition).forEach(it -> it.setDependsOn("databaseStartupValidator"));
        };
    }

    @Bean
    public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
        DatabaseStartupValidator dsv = new DatabaseStartupValidator();
        dsv.setDataSource(dataSource);
        dsv.setValidationQuery(DatabaseDriver.MYSQL.getValidationQuery());
        return dsv;
    }
}

以下是数据库配置类:

@Configuration
public class DatasourceConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(DatasourceConfig.class);

    @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
                    .driverClassName("com.mysql.cj.jdbc.Driver")
                    .url("jdbc:mysql://myDbUrl").username("myUserName").password("myPassword")
                    .build();

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题