无法使用 Postgres DB 启动 Grails 3.3.8 应用程序

Posted

技术标签:

【中文标题】无法使用 Postgres DB 启动 Grails 3.3.8 应用程序【英文标题】:Unable to start Grails 3.3.8 application with Postgres DB 【发布时间】:2019-04-30 01:07:46 【问题描述】:

我开始将我的应用程序从 Grails 2.5.5 迁移到在 Postgres DB 上运行的 Grails 3.3.8,但在 IntelliJ 中启动应用程序时出现错误。以下是详细信息:

错误:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.hibernate.boot.internal.MetadataBuildingContextRootImpl.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;Lorg/hibernate/boot/spi/ClassLoaderAccess;Lorg/hibernate/boot/spi/InFlightMetadataCollector;)V
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1181)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1075)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:225)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:703)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:84)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:393)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:380)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
    at com.mhz.tascr.Application.main(Application.groovy:8)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError

build.gradle:

buildscript 
    repositories 
        mavenLocal()
        mavenCentral()
        maven  url "https://repo.grails.org/grails/core" 
        maven  url "http://repo.grails.org/grails/plugins/" 
    
    dependencies 
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "org.grails.plugins:hibernate5:$gormVersion-".RELEASE""
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.8"
    


repositories 
    mavenLocal()
    mavenCentral()

    maven  url "https://repo.grails.org/grails/core" 
    maven  url "http://repo.grails.org/grails/plugins/" 


dependencies 
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.3.6.Final"
    compile "org.hibernate:hibernate-ehcache:5.3.6.Final"

    runtime "postgresql:postgresql:9.0-801.jdbc4" // POSTGRES

application.yml:

hibernate:
    cache:
        queries: false
        use_second_level_cache: true
        use_query_cache: false
        region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.postgresql.Driver
    dialect: org.hibernate.dialect.PostgreSQLDialect
environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:postgresql://localhost:5432/dev_test
            username: XXXX
            password: XXXX
            logSql: false

gradle.properties:

grailsVersion=3.3.8
gormVersion=6.1.10.RELEASE
gradleWrapperVersion=3.5

我已遵循 grails 迁移指南,并给出了正确的依赖项和 Postgres 方言。我还在外部库中看到了 postgresql-9.0-801.jdbc4.jar,因此无法找出错误原因。

谢谢

【问题讨论】:

getting errors while starting the application in IntelliJ;那么它是否从控制台正确启动? @Michal_Szulc 不,我没有尝试过。但问题与我给出的不兼容依赖项有关。我现在已经在下面发布了我的答案。 【参考方案1】:

按照@erichelgeson 的建议,我升级了我的 jdbc 驱动程序,但还必须进行一些其他更改,例如添加 org.jadira.usertype:usertype.core:5.0.0.GA 并将 region.factory_class 更改为 org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 以使其完全正常工作。

build.gradle:

buildscript 
    repositories 
        mavenLocal()
        mavenCentral()
        maven  url "https://repo.grails.org/grails/core" 
        maven  url "http://repo.grails.org/grails/plugins/" 
    
    dependencies 
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.8"
    


repositories 
    mavenLocal()
    mavenCentral()

    maven  url "https://repo.grails.org/grails/core" 
    maven  url "http://repo.grails.org/grails/plugins/" 


dependencies 
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails.plugins:hibernate5:$gormVersion-".RELEASE""
    compile "org.hibernate:hibernate-ehcache"
    compile "org.jadira.usertype:usertype.core:5.0.0.GA"

    runtime "org.postgresql:postgresql:42.2.5" // POSTGRES

application.yml:

hibernate:
    cache:
        queries: false
        use_second_level_cache: true
        use_query_cache: true
        region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

【讨论】:

【参考方案2】:

您使用的 jdbc 驱动程序是 2010 年的 - 使用较新的驱动程序,例如 https://mvnrepository.com/artifact/org.postgresql/postgresql/42.2.5

GORM 6.1 最高支持 Hibernate 5.2 - 您使用的是 Hibernate 5.3。

【讨论】:

以上是关于无法使用 Postgres DB 启动 Grails 3.3.8 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Prisma 无法验证数据库服务器

GCP SQL Postgres 权限问题:无法使用生成的 symfony db 使用 postgres 用户运行查询

Spring Batch 无法通过 DB (postgres) 获取作业锁

无法使用 Postgres、Docker Compose 和 Psycopg2 将主机名“db”转换为地址

无法在 docker-compose 中使用 Postgres DB - ECONNREFUSED

Docker Compose 无法从数据库 (jdbc:postgresql://db:5432/postgres) 为用户“postgres”获取连接:连接尝试失败