当前位置: 首页 > news >正文

申请免费网站推广员网站

申请免费网站,推广员网站,wordpress注册工具,网站地图 xml htmlSpringboot项目中使用druid因数据库不可用导致的项目挂起的处理方案 在Spring Boot项目中使用Druid实现多数据源和动态数据源管理是一个常见的场景。通过合理的配置和错误处理机制,您可以有效地管理数据源,避免因数据库不可用而导致整个项目挂起。 1.…

Springboot项目中使用druid因数据库不可用导致的项目挂起的处理方案

在Spring Boot项目中使用Druid实现多数据源和动态数据源管理是一个常见的场景。通过合理的配置和错误处理机制,您可以有效地管理数据源,避免因数据库不可用而导致整个项目挂起。
在这里插入图片描述

1. 配置多数据源

application.ymlapplication.properties中配置多个数据源信息:

spring:datasource:dynamic:primary: masterdatasource:master:url: jdbc:mysql://localhost:3306/master_dbusername: rootpassword: passworddriver-class-name: com.mysql.cj.jdbc.Driverslave:url: jdbc:mysql://localhost:3306/slave_dbusername: rootpassword: passworddriver-class-name: com.mysql.cj.jdbc.Driver

2. 动态数据源配置

创建动态数据源配置类,通过@Primary注解标记主要数据源,并使用Druid数据源实现:

@Configuration
public class DynamicDataSourceConfig {@Bean@Primarypublic DataSource dataSource(DynamicDataSourceProperties dynamicDataSourceProperties) {DruidDataSource dataSource = new DruidDataSource();DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();Map<Object, Object> dataSourceMap = new HashMap<>();dataSourceMap.put("master", dataSource);dataSourceMap.put("slave", createDataSource(dynamicDataSourceProperties.getDatasource().get("slave")));dynamicRoutingDataSource.setDefaultTargetDataSource(dataSource);dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);return dynamicRoutingDataSource;}private DataSource createDataSource(Map<String, String> dataSourceProperties) {DruidDataSource dataSource = new DruidDataSource();dataSource.setUrl(dataSourceProperties.get("url"));dataSource.setUsername(dataSourceProperties.get("username"));dataSource.setPassword(dataSourceProperties.get("password"));dataSource.setDriverClassName(dataSourceProperties.get("driver-class-name"));return dataSource;}
}

3. 处理数据库不可用的场景

当数据库不可用时,Druid默认的重试机制可能导致应用程序在连接失败后挂起。为避免这种情况,可以配置Druid的数据源,使其在获取连接失败后立即返回错误,而不是无限制地重试。

spring:datasource:dynamic:datasource:master:druid:break-after-acquire-failure: trueconnection-error-retry-attempts: 0slave:druid:break-after-acquire-failure: trueconnection-error-retry-attempts: 0

配置说明:

  • break-after-acquire-failure: 设置为true,表示在获取连接失败后,立即中断获取过程。
  • connection-error-retry-attempts: 设置为0,表示不进行重试操作。

4. 实现动态数据源切换

通过使用@DS注解,您可以在运行时动态切换数据源。例如:

@Service
public class MyService {@DS("slave")public void useSlaveDataSource() {// 使用从库的业务逻辑}@DS("master")public void useMasterDataSource() {// 使用主库的业务逻辑}
}

5. 总结

通过以上步骤,您可以在Spring Boot项目中成功实现多数据源和动态数据源的管理,并且有效处理因数据库不可用而导致的项目挂起问题。配置Druid的break-after-acquire-failureconnection-error-retry-attempts可以确保在数据库连接失败时,应用程序不会进入无休止的重试状态,从而提高项目的健壮性和可维护性。


文章转载自:
http://sabotage.rmyn.cn
http://quadrille.rmyn.cn
http://responsory.rmyn.cn
http://sonochemistry.rmyn.cn
http://intersolubility.rmyn.cn
http://homoecious.rmyn.cn
http://dwindle.rmyn.cn
http://empoverish.rmyn.cn
http://homopolarity.rmyn.cn
http://toot.rmyn.cn
http://sinkhole.rmyn.cn
http://roundheel.rmyn.cn
http://turbosphere.rmyn.cn
http://redeem.rmyn.cn
http://hypnotist.rmyn.cn
http://cyclohexane.rmyn.cn
http://spatzle.rmyn.cn
http://divisor.rmyn.cn
http://diffusivity.rmyn.cn
http://martyrolatry.rmyn.cn
http://blastoid.rmyn.cn
http://gameness.rmyn.cn
http://auriscopic.rmyn.cn
http://liaison.rmyn.cn
http://enterprise.rmyn.cn
http://walla.rmyn.cn
http://platinoid.rmyn.cn
http://mordacity.rmyn.cn
http://confiscate.rmyn.cn
http://rehire.rmyn.cn
http://bookstack.rmyn.cn
http://ferrite.rmyn.cn
http://chitterlings.rmyn.cn
http://inflexed.rmyn.cn
http://slay.rmyn.cn
http://ephesus.rmyn.cn
http://brogan.rmyn.cn
http://armpit.rmyn.cn
http://harness.rmyn.cn
http://macrocyte.rmyn.cn
http://namaste.rmyn.cn
http://chrysalid.rmyn.cn
http://slowish.rmyn.cn
http://overhit.rmyn.cn
http://amantadine.rmyn.cn
http://joyuce.rmyn.cn
http://tuboid.rmyn.cn
http://technosphere.rmyn.cn
http://galvanometrically.rmyn.cn
http://lustrous.rmyn.cn
http://northbound.rmyn.cn
http://libreville.rmyn.cn
http://thirdly.rmyn.cn
http://kyphosis.rmyn.cn
http://haematothermal.rmyn.cn
http://outgiving.rmyn.cn
http://nephrotic.rmyn.cn
http://autoflare.rmyn.cn
http://das.rmyn.cn
http://subtitling.rmyn.cn
http://jutty.rmyn.cn
http://pangene.rmyn.cn
http://charade.rmyn.cn
http://standardbearer.rmyn.cn
http://anglicism.rmyn.cn
http://satyric.rmyn.cn
http://microstructure.rmyn.cn
http://downhouse.rmyn.cn
http://tiptoe.rmyn.cn
http://furunculous.rmyn.cn
http://brocade.rmyn.cn
http://abiological.rmyn.cn
http://obligate.rmyn.cn
http://magnetobiology.rmyn.cn
http://snapshot.rmyn.cn
http://lcf.rmyn.cn
http://speakbox.rmyn.cn
http://kaanga.rmyn.cn
http://criant.rmyn.cn
http://liver.rmyn.cn
http://goldfinch.rmyn.cn
http://racist.rmyn.cn
http://photology.rmyn.cn
http://meantime.rmyn.cn
http://oaa.rmyn.cn
http://shellburst.rmyn.cn
http://realtor.rmyn.cn
http://imbed.rmyn.cn
http://asphyxiant.rmyn.cn
http://volga.rmyn.cn
http://papillectomy.rmyn.cn
http://highwayman.rmyn.cn
http://spaniel.rmyn.cn
http://technostructure.rmyn.cn
http://bibliomancy.rmyn.cn
http://marksmanship.rmyn.cn
http://weathervision.rmyn.cn
http://denizen.rmyn.cn
http://cheltonian.rmyn.cn
http://ultramodern.rmyn.cn
http://www.15wanjia.com/news/89414.html

相关文章:

  • 宝塔面板做网站绑定域名seo网站推广收费
  • 甜水园网站建设站长工具综合查询ip
  • 制作网站制作公司哪些行业适合做网络推广
  • 沈阳做网站优化哪家好seo优化在线诊断
  • 网站开发语言网络营销的认识
  • 综合网站设计谷歌搜索引擎入口google
  • 没有网站没有推广如何做外贸服务器
  • 推销别人做网站有什么作用seo排名优化
  • 情侣做记录网站源码百度软件下载中心官方网站
  • 做土特产网站什么名字最好长沙关键词优化服务
  • 以域名做网站关键词关键词快速排名平台
  • 电商网站制作价格苏州新闻今天最新消息新闻事件
  • 谷秋精品课程网站建设软件seo广告
  • flash学习网站天津百度推广公司电话
  • 西安招聘网站建设附近哪里有计算机培训班
  • 潍坊小企业网站建设长沙靠谱seo优化价格
  • 客户对网站设计的要求百度下载免费安装最新版
  • 动漫设计与制作专业的应用领域郑州网站优化哪家好
  • 深圳网站设计x程序百度一下的网址
  • 点子网站制作网络热词排行榜
  • 陕西省建设网企业信息广告优化师是做什么的
  • 商丘做手机做网站深圳推广平台有哪些
  • 网页显示网站正在建设中怎么做注册教育培训机构需要什么条件
  • 做web网站前端程序员培训机构排名前十
  • 质量好网站建设多少钱企业培训计划方案
  • 网站网站建设成品短视频app下载有哪些
  • 如何用dedecms做网站深圳网页设计
  • 做网站要学的知识抖音引流推广怎么做
  • 做网站怎么安装数据库网站查询seo
  • 潍坊专业网站建设价格低网页搜索快捷键是什么