使用Spring Boot连接到数据库时显示错误:
classMethod失败 java.lang.IllegalStateException:无法加载ApplicationContext
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/tomtom/workflow/iris/uitest/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
Caused by:
javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
Caused by:
org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by:
org.postgresql.util.PSQLException: The connection attempt failed.
Caused by:
java.net.SocketTimeoutException: connect timed out
属性:
# PostgreSQL DB - "iris"
spring.datasource.jdbc-url= jdbc:postgresql://172.xx.xx.93:5442/workflow?currentSchema=workflow
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.username=workflow
spring.datasource.password=workflow
# PostgreSQL DB - "accesspoint"
secondary.datasource.jdbc-url=jdbc:postgresql://172.xx.xxx.44:5442/workflow?currentSchema=accesspoint
secondary.datasource.username=dba_admin
secondary.datasource.password=dba_admin
hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
hibernate.hbm2ddl.auto= update
bean的类。对于辅助数据源:
package com.tomtom.workflow.acesspoint;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "accessPointEntityManagerFactory",
transactionManagerRef = "accessPointTransactionManager",
basePackages = { "com.tomtom.workflow.acesspoint" }
)
public class AccessPointDbConfiguration {
@Autowired
private Environment env;
@Bean(name = "accessPointDataSource")
@ConfigurationProperties(prefix = "accesspoint.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "accessPointEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean accessPointEntityManagerFactory() {
/*
* return builder .dataSource(dataSource)
* .packages("com.tomtom.workflow.acesspoint") .persistenceUnit("baseline_info")
* .build();
*/
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.tomtom.workflow.acesspoint" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean(name = "accessPointTransactionManager")
public PlatformTransactionManager accessPointTransactionManager(
@Qualifier("accessPointEntityManagerFactory") EntityManagerFactory accessPointEntityManagerFactory) {
return new JpaTransactionManager(accessPointEntityManagerFactory);
}
}
对于Primary数据源,我已经编写了此类。
package com.tomtom.workflow.iris.uitest;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {
"com.tomtom.workflow.iris.uitest" })
@Profile(Profiles.DATABASE)
class DatabaseConfiguration {
@Autowired
private Environment env;
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.tomtom.workflow.iris.uitest.db.model" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
我已经通过pg admin连接了两个数据库,并且它们都处于活动状态。 请帮忙。