【发布时间】:2017-04-17 12:22:51
【问题描述】:
tomcat 出现以下错误:
09:06:18,168 WARNING [org.apache.tomcat.jdbc.pool.PooledConnection] (ServerService Thread Pool -- 6) Not loading a JDBC driver as driverClassName property is null.
09:06:18,170 SEVERE [org.apache.tomcat.jdbc.pool.ConnectionPool] (ServerService Thread Pool -- 6) Unable to create initial connections of pool.: java.sql.SQLException: The url cannot be null
我在 JBOSS 和嵌入式 tomcat 服务器中都运行过这个,但仍然出现这个错误。我什至从war中取出jar文件并从JBOSS运行它,仍然得到同样的错误。
我能够创建 EntityMangers,但在创建它们之前我得到了上述错误。该程序继续运行而不是抱怨一个类不是托管类型。但是那些@Entities 正在被扫描。
我在使用 JBOSS 时遇到同样的错误:
09:06:18,171 WARN [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (ServerService Thread Pool -- 6) HHH000342: Could not obtain connection to query metadata : The url cannot be null
09:06:18,183 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 6) HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
09:06:18,199 INFO [org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl] (ServerService Thread Pool -- 6) HHH000422: Disabling contextual LOB creation as connection was null
数据源:
@Configuration
@PropertySource("classpath:application.yml")
public class MainDataSourceConfig {
/*******************************
* Datasource *
* *****************************/
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.dataSource.Main")
public DataSource mainDataSource() {
return DataSourceBuilder.create().build();
}
/*******************************
* Transaction manager *
* *****************************/
@Bean
@Primary
DataSourceTransactionManager transactionManager(@Qualifier("mainDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("mainDataSource")DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName("mainEntityManger");
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactory.setJpaDialect(new HibernateJpaDialect());
entityManagerFactory.setPackagesToScan("com.customers.domain");
entityManagerFactory.setJpaProperties(additionalProperties());
return entityManagerFactory;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.ddl-auto","none");
return properties;
}
}
第二个数据源
@Configuration
@PropertySource("classpath:application.yml")
public class SecondDataSourceConfig {
/*******************************
* Datasource *
* *****************************/
@Bean
@ConfigurationProperties(prefix="spring.dataSource.Second")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
/*******************************
* Transaction manager *
* *****************************/
@Bean
DataSourceTransactionManager transactionManager(@Qualifier("secondDataSource") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
@Bean
public LocalContainerEntityManagerFactoryBean keyviewEntityMangerFactory(@Qualifier("secondDataSource") DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName("secondEntityManger");
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactory.setJpaDialect(new HibernateJpaDialect());
entityManagerFactory.setPackagesToScan("com.statements.domain");
entityManagerFactory.setJpaProperties(additionalProperties());
return entityManagerFactory;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.ddl-auto","none");
return properties;
}
}
application.yml
#Spring Boot Config for Oracle
spring:
dataSource:
Main:
url: [url]
username: [username]
password: [password]
driverClassName: oracle.jdbc.OracleDriver
Second:
url: [url]
username: [username]
password: [password]
driverClassName: oracle.jdbc.OracleDriver
tomcat:
min-idle: 1
# Spring Boot Actuator settings
#https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
endpoints.health.sensitive: false
#management.security.enabled: true
management.context-path: /healthCheck
endpoints.info.id: info
endpoints.info.sensitive: false
endpoints.info.enabled: true
info.app.name: Request
info.app.description: Request Description
info.app.version: 0.0.1-SNAPSHO
我已经做了一个多星期了,我无法弄清楚为什么它说 url 为空,后来创建 entityManagers 时说我的一个域包不是托管类型。扫描第二组软件包似乎有问题。我确认了更改类名,因为我注意到它是按字母顺序编译的,当我这样做时,它改变了它抱怨的包。 #note:每次我运行同一个包时,它都会选择一个不同的类,并且它们是 @Entity 注释的。
我也在 main 方法上使用@EntityScan、@ComponentScan、@SpringBootApplication、@EnableAutoConfiguration。
注意:当我删除第二个数据源时,一切正常。只有当我引入第二个时才会发生这种情况。
------------更新1--------- --------------
我只是将数据源信息放入两个不同的yml 文件中,但仍然出现相同的错误。然后我决定把主要的数据源拿出来,把第二个作为项目中唯一的一个来实现。 那么我得到了同样的错误。但是 URI 和一切都是正确的,不知道为什么会这样。
【问题讨论】:
标签: hibernate tomcat spring-boot jboss