springboot 数据库断线自动重连

springboot 数据库断线自动重连,第1张

max-idle: 10

max-lifetime: 10000

min-idle: 5

initial-size: 5

test-on-borrow: false

test-while-idle: true

time-between-eviction-runs-millis: 18800

jdbc-interceptors: ConnectionStateSlowQueryReport(threshold=5)

validation-query: SELECT 1 FROM DUAL

采用spring配置文件

<bean id="dataSource_soccer_bet"

class="org.apache.commons.dbcp.BasicDataSource" singleton="true" destroy-method="close">

<property name="driverClassName">

<value>com.mysql.jdbc.Driver</value>

</property>

<property name="url">

<value>jdbc:mysql://localhost:3306/hoop_soccer_bet?autoReconnect=true&useUnicode=true&characterEncoding=

方案1:

application.properties里面添加即可

spring.datasource.druid.break-after-acquire-failure=true

spring.datasource.druid.connection-error-retry-attempts=1

1

2

1

2

方案2:动态数据源解决方案

DruidDataSource druidDataSource=new DruidDataSource()

druidDataSource.setUrl(dbInfo.getUrl())

druidDataSource.setUsername(dbInfo.getUserName())

druidDataSource.setPassword(dbInfo.getPassword())

druidDataSource.setConnectionErrorRetryAttempts(1) //失败后重连次数

druidDataSource.setBreakAfterAcquireFailure(true)

1

2

3

4

5

6

1

2

3

4

5

6

完成了,只会试一次,不成功不会无限尝试


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10869304.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存