如何使用Grails 3.0配置PostgreSQL?

时间:2016-01-15 18:55:30

标签: java postgresql grails jdbc intellij-idea

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

这是我的dataSource:

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

我错过了什么?

4 个答案:

答案 0 :(得分:4)

数据来源

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编号。 希望我帮忙。干杯!

答案 1 :(得分:1)

您的配置中似乎缺少标签

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

数据源之后的所有内容都需要缩进。

答案 2 :(得分:0)

我不知道它是否得到了解答但我发现使用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" 
        }
    }

}

希望这可以帮助每个人解决同样的问题。

干杯

答案 3 :(得分:-1)

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

我仍然没有想出如何通过IntelliJ正常工作,但至少它在您使用Grails控制台时有效!

注意:这假设您已经正确设置了JDBC驱动程序。我已经为MySQL设置了适当的东西并不停地挖掘,认为我以某种方式设置错误。