【发布时间】:2018-03-21 05:04:47
【问题描述】:
一个多星期以来,我一直在为类似的事情苦苦挣扎。问题是我需要 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;
}
}
问题在于 this 属性:
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连接属性并检查吗?
-
我已经编辑了我得到的异常......但我想知道 tomcat 属性是否解决了这个问题......
-
我在答案部分粘贴了我的输出。你可能想尝试一下。我会说除非你尝试添加连接属性并进行测试,否则你不知道这是否能解决问题.
-
这就是问题的原因...我已经尝试过了,但是没有加载tomcat属性...我怎么知道?因为我输出的是dataSource,这个属性的值不存在。
标签: java tomcat spring-boot datasource