具有两个数据源自动连接的Spring Boot

时间:2017-10-23 08:14:44

标签: spring spring-boot

我有带两个数据源的spring boot应用程序,其中一个每天晚上重新启动,我需要解决自动连接问题。我做了一些研究但是,任何解决方案都不起作用

我的dev.properties

datasource.post.url=jdbc:postgresql://…
datasource.post.driverClassName=org.postgresql.Driver
datasource.post.username=…
datasource.post.password=…

datasource.orl.url=jdbc:oracle:thin:@localhost:1521/spring
datasource.orl.driverClassName=oracle.jdbc.OracleDriver
datasource.orl.username=….
datasource.orl.password=…

我尝试添加到dev.properties

datasource.orl.test-on-borrow=true
datasource.orl.validation-query=SELECT 1

没用。我还将建议的版本添加到application.properties

spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.validationQuery=SELECT 1

此版本同样不起作用,我甚至没有添加推荐的自动连接

jdbc:oracle:thin:@localhost:1521/spring?autoReconnect=true

但是当我重新启动tomcat时,我得到错误

: Error creating bean with name ‘orlEntityManager' defined in class path resource [com/xxx/config/OrlDSConfiguration.class]:Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

编辑我扩展stackTrace信息和我的Orl datacourse配置类

@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "com.xxx.orl.repository",
        entityManagerFactoryRef = "orlEntityManager",
        transactionManagerRef = "orlTransactionManager")
public class OrlDSConfiguration {

    @Bean(name = "orlDS")
    @ConfigurationProperties(prefix = "datasource.orl")
    public DataSource orlDataSource() {
        return DataSourceBuilder.create()
                .build();
    }

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Bean(name = "orlEntityManager")
    @Primary
    @PersistenceContext(unitName = "orl")
    public LocalContainerEntityManagerFactoryBean orlContainerEMFactory(@Qualifier("orlDS") DataSource ds, EntityManagerFactoryBuilder builder, JpaProperties jpaProperties) {
        Map<String, String> customProperties = jpaProperties.getHibernateProperties(ds);
        return builder.dataSource(ds)
                .persistenceUnit("orl")
                .properties(jpaProperties.getProperties())
                .properties(customProperties)
                .packages("com.xxx.orl.domain")
                .build();
    }

    @Bean(name = "orlTransactionManager")
    public PlatformTransactionManager orlTransactionManager(
            @Qualifier("orlEntityManager") EntityManagerFactory orlContainerEMFactory) {
        return new JpaTransactionManager(orlContainerEMFactory);
    }

}

0 个答案:

没有答案