Java Spring引导和Hibernate:即使与数据库的连接不可用,也要启动应用程序
我的Spring Boot应用程序需要连接到两个不同的数据库。第一个数据库(主数据库)安装在与本地主机应用程序相同的服务器上,另一个数据库(辅助数据库)安装在远程服务器上,但并不总是可用(用于维护、备份、测试等) 我使用以下配置(application.properties) 初始化应用程序时,hibernate会尝试连接到两个数据库。如果第二个数据库当时不可用,将引发异常并中止应用程序初始化 是否有任何属性可用于防止应用程序在启动时中止Java Spring引导和Hibernate:即使与数据库的连接不可用,也要启动应用程序,java,spring,hibernate,spring-boot,database-connection,Java,Spring,Hibernate,Spring Boot,Database Connection,我的Spring Boot应用程序需要连接到两个不同的数据库。第一个数据库(主数据库)安装在与本地主机应用程序相同的服务器上,另一个数据库(辅助数据库)安装在远程服务器上,但并不总是可用(用于维护、备份、测试等) 我使用以下配置(application.properties) 初始化应用程序时,hibernate会尝试连接到两个数据库。如果第二个数据库当时不可用,将引发异常并中止应用程序初始化 是否有任何属性可用于防止应用程序在启动时中止 我该怎么办?当会话工厂启动时,Hibernate需要连接
我该怎么办?当
会话工厂
启动时,Hibernate需要连接到数据库,以便我可以从数据库连接中提取数据库元数据
使用数据库元数据
,它需要找出:
- 当前目录和架构
- 如何限定标识符
- 如果数据库支持临时表
- 如果DDL导致事务提交
- 如果驱动程序支持可滚动的
ResultSet
- 如果驱动程序支持批量更新
- 如果驱动程序返回标识列的生成键
当会话工厂
初始化时,此信息将被解析,因此,当相关数据库也可用时,您最好懒散地启动新的微服务。您可以尝试在主方法中捕获异常。此链接可能会有所帮助。是否有示例?在我的应用程序的主方法中,它会是一个“try/catch”吗?它会是怎样的呢?把行SpringApplication.run(YourApplication.class,args)放进去;在两者之间,尝试捕捉。我认为它会起作用。检查以下答案:
# 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