gpt4 book ai didi

java - Spring 多个数据源不读取某些应用程序属性

转载 作者:行者123 更新时间:2023-11-28 22:25:37 25 4
gpt4 key购买 nike

我已经为类似的事情苦苦挣扎了一个多星期。问题是我需要 2 个数据源,因为我的应用程序有自己的数据库,但还需要为另一个数据库检索信息。

首先,我决定与 2 个数据库进行通信,一开始我遇到了问题,但现在有时当应用程序尝试连接到其中一个数据库时,使用 jdbcTemplate 的数据库可能会在 1 小时或更短时间后关闭连接并得到一个异常,不能再建立连接。我想从 tomcat 池中配置一些属性将有助于解决这个问题,但是数据源没有读取这些属性。所以这是我的应用程序属性和数据库配置类,以澄清所有这些内容。

应用程序属性:

spring.datasource.url=jdbc:postgresql://localhost:5433/firstDB
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=user1
spring.datasource.password=password1
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQL92Dialect
spring.jpa.hibernate.ddl-auto=update

# --------------------
# CONFIGURACION SECOND
# --------------------

spring.sia.datasource.url=jdbc:postgresql://192.168.1.202:5432/secondDB
spring.sia.datasource.tomcat.remove-abandoned=true
spring.sia.datasource.tomcat.initial-size=20
spring.sia.datasource.driver-class-name=org.postgresql.Driver
spring.sia.datasource.username=user2
spring.sia.datasource.password=password2

数据库配置:

@Configuration
public class DbConfig {

@Bean(name="firstDb")
@Primary
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource firstDataSource() {
return firstDataSourceProperties().initializeDataSourceBuilder().build();
}

@Bean
@ConfigurationProperties(prefix="spring.sia.datasource")
public DataSourceProperties secondDataSourceProperties(){
return new DataSourceProperties();
}

@Bean(name="siaDb")
public DataSource secondDataSource() {
return secondDataSourceProperties().initializeDataSourceBuilder().build();
}

@Bean(name="siaJdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("siaDb") DataSource dataSource) {

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

jdbcTemplate.setResultsMapCaseInsensitive(true);

return jdbcTemplate;
}

}

问题是这个属性:

spring.sia.datasource.tomcat.remove-abandoned=true
spring.sia.datasource.tomcat.initial-size=20

没有阅读第二个数据源,也许我遗漏了一些配置类。

我需要创建手动数据源吗??如果是这样,使用 @ConfigurationProperties 注释和调用 datasourcebuilder 有什么意义??

我会感谢你的所有帮助,因为我需要先弄清楚 tomcat 池属性是否解决了这个问题,或者可能有什么不同。

这是在尝试访问应用程序的端点后发生的异常。

org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:342) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:366) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:212) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:97) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:716) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:726) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:776) [spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at com.swargos.api.repositories.ServicioSiaRepositoryImpl.getAllServiciosByUsuario(ServicioSiaRepositoryImpl.java:112) [classes!/:na]
at com.swargos.api.repositories.ServicioSiaRepositoryImpl$$FastClassBySpringCGLIB$$f87228d2.invoke(<generated>) [classes!/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) [spring-aop-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) [spring-tx-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at com.swargos.api.repositories.ServicioSiaRepositoryImpl$$EnhancerBySpringCGLIB$$a929bedc.getAllServiciosByUsuario(<generated>) [classes!/:na]
at com.swargos.api.services.ServicioSiaServiceImpl.findServiciosByUsuario(ServicioSiaServiceImpl.java:37) [classes!/:na]
at com.swargos.api.controllers.ServicioSiaController.getServiciosCurrentUser(ServicioSiaController.java:54) [classes!/:na]
at sun.reflect.GeneratedMethodAccessor98.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) [spring-web-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) [spring-web-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [javax.servlet-api-3.1.0.jar!/:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]

谢谢

最佳答案

这些错误可能很难解决。你能发布你得到的异常的堆栈跟踪吗?

作为开始,您可以尝试为数据源和检查提供 tomcat 连接属性。

我最近确实为一个项目创建了 2 个数据源,并提供了两个数据源的 tomcat 连接属性。

db1.database.maximumPoolSize=100
db1.database.connectionTimeout=30000
db1.database.validationTimeout=5000
db1.database.idleTimeout=60000
db1.database.leakDetectionThreshold=400000
db1.database.maxLifetime=600000
db1.database.minIdle=-1
db1.database.poolName=hikari-app
db1.database.isRegisterMbeans=true

db2.database.maximumPoolSize=100
db2.database.connectionTimeout=30000
db2.database.validationTimeout=5000
db2.database.idleTimeout=60000
db2.database.leakDetectionThreshold=400000
db2.database.maxLifetime=600000
db2.database.minIdle=-1
db2.database.poolName=hikari-app2
db2.database.isRegisterMbeans=true

关于java - Spring 多个数据源不读取某些应用程序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com