我正在尝试手动设置SPring Boot应用程序中的所有属性,因为我将最终使用不同的DataSource用于常规JDBC。但这是我的代码:
@Bean
@Primary
public DataSource getJpaDatasource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(hibernateConfiguration.getDriverClassName());
dataSource.setUrl(hibernateConfiguration.getDatabaseUrl());
dataSource.setUsername(hibernateConfiguration.getUsername());
dataSource.setPassword(hibernateConfiguration.getPassword());
return dataSource;
}
现在我正在使用遗留代码,需要创建实体工厂,我想将Hikari用作连接池。这是代码:
@Bean
public EntityManagerFactory getEntityManager() {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(getJpaDatasource());
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
Map<String, String> hibernateProperties = new HashMap<>();
hibernateProperties.put("hibernate.dialect",hibernateConfiguration.getProperties().get("hibernate.dialect"));
hibernateProperties.put("hibernate.show-sql",hibernateConfiguration.getProperties().get("show-sql"));
hibernateProperties.put("hibernate.format-sql",hibernateConfiguration.getProperties().get("format-sql"));
hibernateProperties.put("hibernate.id.new_generator_mappings",hibernateConfiguration.getProperties().get("id.new_generator_mappings"));
hibernateProperties.put("hibernate.ejb.naming_strategy",hibernateConfiguration.getProperties().get("ejb.naming_strategy"));
hibernateProperties.put("hibernate.hikari.minimumIdle",hibernateConfiguration.getConnectionPool().get("hibernate.hikari.minimumIdle"));
hibernateProperties.put("hibernate.hikari.maximumPoolSize", hibernateConfiguration.getConnectionPool().get("hibernate.hikari.maximumPoolSize"));
hibernateProperties.put("hibernate.hikari.idleTimeout", hibernateConfiguration.getConnectionPool().get("hibernate.hikari.idleTimeout"));
hibernateProperties.put("hibernate.connection.provider_class",hibernateConfiguration.getConnectionPool().get("hibernate.connection.provider_class"));
localContainerEntityManagerFactoryBean.setJpaPropertyMap(hibernateProperties);
localContainerEntityManagerFactoryBean.afterPropertiesSet();
EntityManagerFactory ef = (EntityManagerFactory)localContainerEntityManagerFactoryBean.getObject();
return ef;
}
现在没有任何东西返回null或任何东西,我只是得到这个错误:
nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
我缺少什么想法?
修改 为了让所有明显的方法脱颖而出:
这就是我得到的:
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
这看似简单,但我无法想出这个!
答案 0 :(得分:0)
当通过Spring手动覆盖事物时,记住你必须覆盖所有东西是很重要的。我忘了的是TransactionManager。这就是全部:
@Bean
public JpaTransactionManager jpaTransactionManager() {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = this.getEntityManagerFactoryBean();
EntityManagerFactory ef = (EntityManagerFactory)localContainerEntityManagerFactoryBean.getObject();
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(ef);
return transactionManager;
}
我不知道连接池是否正常工作,但我能够从数据库中检索数据。