首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring和Hibernate:即使数据库连接不可用,也可以启动应用程序

问Spring和Hibernate:即使数据库连接不可用,也可以启动应用程序
EN

Stack Overflow用户
提问于 2016-12-06 22:46:11
回答 1查看 2.7K关注 0票数 2

我的Spring应用程序需要连接到两个不同的数据库。第一个数据库(main)与本地主机应用程序和远程服务器上的另一个数据库(次要数据库)安装在同一台服务器上,而这个数据库并不总是可用的(用于维护、备份、测试等)。

我使用以下配置(application.properties)。

代码语言:javascript
复制
# main connection
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/?autoReconnect=true&verifyServerCertificate=false&useSSL=false&requireSSL=false
spring.datasource.username=emater
spring.datasource.password=emater

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# secondary connection
planejamento.datasource.driverClassName=com.mysql.jdbc.Driver
planejamento.datasource.url=jdbc:mysql://10.22.1.4/?verifyServerCertificate=false&useSSL=false&requireSSL=false
planejamento.datasource.username=emater
planejamento.datasource.password=emater
planejamento.datasource.testWhileIdle = false

#config hibernate
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQLSpatial56Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
spring.jpa.show-sql=true
spring.jpa.format-sql=true
spring.jpa.use-sql-comments=true
spring.jpa.hibernate.enable_lazy_load_no_trans=true

初始化应用程序时,hibernate尝试连接到两个数据库。如果第二个数据库当时不可用,则抛出异常并中止应用程序初始化。

是否有任何属性可用于防止应用程序在启动时中止?

我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-08 15:20:50

Hibernate需要在SessionFactory时连接到DB,以便从DB Connection中提取DatabaseMetaData。

对于DatabaseMetaData,它需要找出:

  • 当前目录和架构
  • 如何限定标识符
  • 如果DB支持临时表
  • 如果DDL导致事务提交
  • 如果驱动程序支持可滚动的ResultSet
  • 如果驱动程序支持批处理更新
  • 如果驱动程序返回生成的标识列密钥

这个信息在初始化SessionFactory时被解析,所以最好是懒洋洋地启动一个新的MicroService,同时关联的数据库也是不可缺少的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41006337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档