我的Spring Boot应用程序需要连接到两个不同的数据库。第一个数据库(主数据库)与localhost应用程序安装在同一服务器上,另一个数据库(辅助数据库)安装在远程服务器上,并且并非始终可用(用于维护,备份,测试等)。
我使用以下配置(application.properties)。
# 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需要连接到数据库,SessionFactory
以便我可以DatabaseMetaData
从数据库中提取Connection
。
使用DatabaseMetaData
,它需要找出:
ResultSet
SessionFactory
初始化时会解决此信息,因此最好在关联数据库也可用的情况下延迟启动新的MicroService。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句