【问题标题】:How do I configure PostgreSQL with Grails 3.0?如何使用 Grails 3.0 配置 PostgreSQL?
【发布时间】:2016-01-15 18:55:29
【问题描述】:

我使用 IntelliJ IDEA 15.0.2 作为 IDE。我创建了一个 Grails 3.0 应用程序并对其进行了一些更改以配置 PostgreSQL。

这是我的数据源:

dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: postgres
password: root

environments:
development:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
test:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
production:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
        properties:
            jmxEnabled: true
            initialSize: 5
            maxActive: 50
            minIdle: 5
            maxIdle: 25
            maxWait: 10000
            maxAge: 600000
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 60000
            validationQuery: SELECT 1
            validationQueryTimeout: 3
            validationInterval: 15000
            testOnBorrow: true
            testWhileIdle: true
            testOnReturn: false
            jdbcInterceptors: ConnectionState
            defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

在我的build.gradle 中,我添加了runtime "postgresql:postgresql:9.4-1207.jdbc4"

但是当我运行时会出错:

ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: org.postgresql.Driver

我错过了什么?

【问题讨论】:

  • 类路径中是否有 postgresql JDBC 驱动程序?
  • 请接受一个答案。

标签: java postgresql grails jdbc intellij-idea


【解决方案1】:

数据来源

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "org.postgresql.Driver"
    username = "postgres"
    password = "xxx"

构建配置

dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
        // runtime 'mysql:mysql-connector-java:5.1.29'
        // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
        runtime "org.postgresql:postgresql:9.4.1208.jre7"
        test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
    }

根据您的 db numbe psql 版本更改 jre 编号。 希望我有所帮助。干杯!

【讨论】:

  • 你救了一条命!谢谢!
【解决方案2】:

您的配置中似乎缺少一个选项卡

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.postgresql.Driver
    username: postgres
    password: root

datasource 之后的所有内容都需要缩进。

【讨论】:

    【解决方案3】:

    我不知道它是否被回答,但我发现使用 application.yml 并没有让我访问数据源配置,而是假定 H2 驱动程序。

    build.gradle(仅依赖块):

    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:grails-dependencies"
    compile "org.grails:grails-web-boot"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:hibernate4"
    compile "org.hibernate:hibernate-ehcache"
    
    runtime "postgresql:postgresql:9.4.1208-atlassian-hosted"
    
    compile "org.grails.plugins:spring-security-core:3.0.4"
    
    console "org.grails:grails-console"
    profile "org.grails.profiles:web:3.1.6"
    runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"
    
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"
    testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
    testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
    

    从 application.yml 中删除与数据库相关的所有内容,改为使用 application.groovy:

    dataSource{
        pooled= true
        jmxExport=true
        driverClassName= 'org.postgresql.Driver'
        username= 'xxxx'
        password= 'xxxx'    }
    
    environments{   
        development{
            dataSource{
                dbCreate= 'create-drop'
                url= "jdbc:postgresql://localhost:5432/xxx"
                logSql= true
                hibernate.default_schema= "template_dm" 
            }
        }
        test{
            dataSource{
                dbCreate= 'create-drop'
                url= "jdbc:postgresql://localhost:5432/xxx"
                logSql= true
                hibernate.default_schema= "template_dm" 
            }
        }
        production{
            dataSource{
                dbCreate= 'create-drop'
                url= "jdbc:postgresql://localhost:5432/xxx"
                logSql= true
                hibernate.default_schema= "template_dm" 
            }
        }
    
    }
    

    希望这对遇到相同问题的每个人都有帮助。

    干杯

    【讨论】:

      【解决方案4】:

      对于那些正在寻找 Grails 4/5 版本的人:
      implementation "org.postgresql:postgresql:42.3.2"

      dataSource:
      pooled: true
      jmxExport: true
      driverClassName: org.postgresql.Driver
      username: postgres
      password: ''
      

      最后

      url: jdbc:postgresql://localhost:5432/db_name
      

      【讨论】:

        【解决方案5】:

        使用grails run-app 运行 Grails 应用程序,而不是通过 IntelliJ 运行它。我有这个issue too with MySQL

        我还没有弄清楚如何通过 IntelliJ 正常工作,但至少在使用 Grails 控制台时它可以工作!

        注意:这是假设您已经正确设置了 JDBC 驱动程序。我已经为 MySQL 正确设置了我的设置,并一直在四处寻找,以为我设置错了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多