使用MySQL设置Grails 3.1.0

时间:2016-03-24 17:44:11

标签: mysql grails jdbc

我正在努力使用本地MySQL数据库(仅在WAMP上)设置我的Grails 3.1.0应用程序,并且我从现有资源中尝试的任何内容都无法正常工作。

以下解决方案均不适合我:

Non-Solution 1

的grails-app / CONF / application.yml:

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: sa
    password:

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://liveip.com/liveDb

的build.gradle:

runtime 'mysql:mysql-connector-java:5.1.36'

非解决方案2

类似,但从this Github starter project复制application.yml和build.gradle文件。

非解决方案3

按照以下方式根据此答案下载MySQL connector jar并在build.gradle文件中引用它:

dependencies {

    ...

    compile files('libs/a.jar')
}

每次尝试后,我都会运行grails clean并在IntelliJ中重建。每种方法都会产生以下堆栈跟踪:

ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver" from ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb;ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb
...
Caused by: java.lang.ClassNotFoundException: Unable to load class: com.mysql.jdbc.Driver" from ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb;ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb
    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:56) ~[tomcat-jdbc-8.0.30.jar:na]
...
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_05]

1 个答案:

答案 0 :(得分:2)

这是我的

的build.gradle:

dependencies {
    runtime 'mysql:mysql-connector-java:5.1.20'

}

现在application.yml:

dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username: username
        password: opendoor_policy
    nextdbsource:
        pooled: true
        jmxExport: true
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username: username
        password: opendoor_policy
        url: jdbc:mysql://localhost:3306/nextdbsource?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
        dbCreate: update

environments:
    development:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
    test:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
    production:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
                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

---

适用于我,它设置了2个数据源