我的Spring应用程序需要连接到两个不同的数据库。第一个数据库(main)与本地主机应用程序和远程服务器上的另一个数据库(次要数据库)安装在同一台服务器上,而这个数据库并不总是可用的(用于维护、备份、测试等)。
我使用以下配置(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尝试连接到两个数据库。如果第二个数据库当时不可用,则抛出异常并中止应用程序初始化。
是否有任何属性可用于防止应用程序在启动时中止?
我该怎么办?
发布于 2016-12-08 15:20:50
Hibernate需要在SessionFactory时连接到DB,以便从DB Connection中提取DatabaseMetaData。
对于DatabaseMetaData,它需要找出:
ResultSet这个信息在初始化SessionFactory时被解析,所以最好是懒洋洋地启动一个新的MicroService,同时关联的数据库也是不可缺少的。
https://stackoverflow.com/questions/41006337
复制相似问题