gpt4 book ai didi

java - Spring启动时出现firebird数据库错误

转载 作者:行者123 更新时间:2023-12-02 11:29:15 25 4
gpt4 key购买 nike

创建类路径资源中定义的名为“entityManagerFactory”的 bean 时出错。我在如何配置 application.properties 时遇到问题。它可以与 H2 完美配合,但与 Firebird 和 SpringBoot 则不然。我是 Spring 的新人。@SpringBootApplication 在 xml 中使用这个 bean 吗?

我在 application.properties 中的连接是

spring.datasource.url:jdbc:firebirdsql://localhost:3050/C:/testdata.fdb
spring.datasource.driverClassName:org.firebirdsql.jdbc.FBDriver
spring.datasource.username:SYSDBA
spring.datasource.password:masterkey
spring.jpa.hibernate.ddl-auto:create
spring.jpa.show-sql: true
spring.jpa.properties.hibernate.format_sql=true
firebird.connection.type=TYPE4

我的 xml 代码是:

<?xml version="1.0" encoding="UTF-8"?>


<bean id="dataSource" class="org.firebirdsql.pool.FBWrappingDataSource">
<property name="database">
<value>${firebird.database}</value>
</property>
<property name="userName">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxPoolSize">
<value>${jdbc.maxPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${jdbc.minPoolSize}</value>
</property>
<property name="maxStatements">
<value>${jdbc.maxStatements}</value>
</property>
<property name="type">
<value>${firebird.connection.type}</value>
</property>
</bean>

我的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.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:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at com.BookStoreDemo.bookStoreDemo.BookStoreDemoApplication.main(BookStoreDemoApplication.java:13) [classes/:na]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:388) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259) ~[hibernate-core-5.2.13.Final.jar:5.2.13.Final]
... 33 common frames omitted

最佳答案

这个问题,org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set ,表明如果连接出现问题,则可能会出现此问题。鉴于您尚未显示属性 firebird.databasejdbc.username 等的存在,这可能是直接原因。正如我昨天在评论中建议的,您可能还想检查日志中是否存在任何错误。

但是,解决此问题的更好方法是删除数据源的 XML 配置。原因有二:

  1. 您指定的 FBWrappingDataSource 有错误(Jaybird 2.2 中已弃用,Jaybird 3 中已删除),建议使用专用连接池库,如 HikariCP
  2. 如果已填充 spring.datasource.urlspring.datasource.driverClassName,Spring 将自动配置合适的数据源(Spring Boot 2 中默认为 HikariCP)。请查阅 Spring Boot 文档以获取进一步的配置(例如管理池中的连接数等)。

举个例子,我在https://start.spring.io/上从头开始创建了一个(Gradle)项目。仅启用 JPA 和 Web,并进行了以下修改:

build.gradle(在依赖项中)中,我添加了对 Jaybird 的依赖项:

compile('org.firebirdsql.jdbc:jaybird-jdk18')

使用 Spring Boot 2.0.0.RELEASE 时,默认为 Jaybird 3.0.3(目前最新)

application.properties中,我设置:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:firebirdsql://localhost/employee
spring.datasource.hikari.data-source-properties.charSet=utf-8
spring.datasource.username=sysdba
spring.datasource.password=masterkey

注意,我使用的是 Firebird 附带的示例 Employee 数据库(定义了别名 employee)。

接下来我创建了一个骨架Employee类:

package example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empNo;

private String firstName;
private String lastName;

public Integer getEmpNo() {
return empNo;
}

public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}

存储库EmployeeRepository:

package example;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
}

还有一个 Controller :

package example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/demo")
public class MainController {

@Autowired
private EmployeeRepository employeeRepository;

@GetMapping(path="/all")
public @ResponseBody
Iterable<Employee> getAllUsers() {
return employeeRepository.findAll();
}
}

启动应用程序并打开http://localhost:8080/demo/all足以查询数据库。如果您检查日志记录,您会发现 Hibernate 将正确选择 Firebird 方言:

[..] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.FirebirdDialect

简而言之,主要解决方案是使用默认的 Spring Boot 配置,更多地依赖 Spring Boot 的自动配置,而不是尝试以“旧”方式(Spring XML 配置等)进行操作。

关于java - Spring启动时出现firebird数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com