gpt4 book ai didi

spring-data-jpa - 如何为 Spring Datasource 创建自定义重试逻辑?

转载 作者:行者123 更新时间:2023-12-04 01:49:25 24 4
gpt4 key购买 nike

我正在连接到 Azure SQL 数据库,我的下一个任务是在连接失败时创建自定义重试逻辑。我希望重试逻辑在启动时(如果需要)以及应用程序运行时出现连接故障时运行。我做了一个测试,我从我的应用程序中删除了 IP 限制,然后在我的应用程序中导致了异常(作为异常(exception))。我想在抛出该异常时进行处理,以便我可以触发一个作业来验证应用程序和服务器是否已正确配置。我正在寻找可以处理这些异常并重试数据库事务的解决方案?

数据源配置

@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder
.create()
.username("username")
.password("password")
.url("jdbc:sqlserver://contoso.database.windows.net:1433;database=*********;user=******@*******;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;")
.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.build();
}

application.properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
spring.jpa.show-sql=true
logging.level.org.springframework.web: ERROR
logging.level.org.hibernate: ERROR
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=1
spring.datasource.tomcat.test-on-borrow=true
spring.jpa.hibernate.ddl-auto=update

最佳答案

以下代码可以帮助您在 Spring Boot 上为数据源创建重试逻辑:

package com.example.demo;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;

@SpringBootApplication
@EnableRetry
public class DemoApplication {

@Order(Ordered.HIGHEST_PRECEDENCE)
private class RetryableDataSourceBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof DataSource) {
bean = new RetryableDataSource((DataSource)bean);
}
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
}

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Bean
public BeanPostProcessor dataSouceWrapper() {
return new RetryableDataSourceBeanPostProcessor();
}
}

class RetryableDataSource extends AbstractDataSource {

private DataSource delegate;

public RetryableDataSource(DataSource delegate) {
this.delegate = delegate;
}

@Override
@Retryable(maxAttempts=10, backoff=@Backoff(multiplier=2.3, maxDelay=30000))
public Connection getConnection() throws SQLException {
return delegate.getConnection();
}

@Override
@Retryable(maxAttempts=10, backoff=@Backoff(multiplier=2.3, maxDelay=30000))
public Connection getConnection(String username, String password)
throws SQLException {
return delegate.getConnection(username, password);
}

}

关于spring-data-jpa - 如何为 Spring Datasource 创建自定义重试逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53948890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com