【问题标题】:How to make MySQL work on grails 2.0如何使 MySQL 在 grails 2.0 上运行
【发布时间】:2012-02-11 23:57:12
【问题描述】:

Grails 2.0 似乎对DataSource.groovy 进行了一些更改,我似乎无法像在 1.3.7 中那样运行 mysql

我做了grails install-dependency mysql:mysql-connector-java:5.1.16 而不仅仅是将.jar 转储到lib 中。我听说这就是这几天的做法。

这是我在DataSource.groovy 中替换的内容:

driverClassName = "org.h2.Driver"
...
url = "jdbc:h2:mem:devDb;MVCC=TRUE"

driverClassName = "com.mysql.jdbc.Driver"
...
url = "jdbc:mysql://localhost:3306/${dbNamer}?autoreconnect=true"

当然将用户名、密码和 dbNamer 更改为有效条目...我做错了什么?是否有涵盖设置 mysql 的 grails 2.0 教程?

我得到这个怪物错误:

| Loading Grails 2.0.0
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application.....
| Compiling 1 source files.....
| Running Grails application
| Error 2012-01-16 21:39:10,134 [Thread-9] ERROR context.GrailsContextLoader  - Error     executing bootstraps: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    Line | Method
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager':     Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run . . . in java.lang.Thread
Caused by SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
->> 1429 | createConnectionFactory in org.apache.commons.dbcp.BasicDataSource
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1371 | createDataSource in     ''
|   1044 | getConnection in     ''
|    334 | innerRun  in java.util.concurrent.FutureTask$Sync
|    166 | run . . . in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run       in java.lang.Thread
Caused by ClassNotFoundException: com.mysql.jdbc.Driver
->>  217 | run       in java.net.URLClassLoader$1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    205 | findClass in java.net.URLClassLoader
|    321 | loadClass in java.lang.ClassLoader
|    266 | loadClass in     ''
|   1420 | createConnectionFactory in org.apache.commons.dbcp.BasicDataSource
|   1371 | createDataSource in     ''
|   1044 | getConnection in     ''
|    334 | innerRun  in java.util.concurrent.FutureTask$Sync
|    166 | run . . . in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run       in java.lang.Thread

【问题讨论】:

    标签: mysql grails datasource grails-orm grails-2.0


    【解决方案1】:

    install-dependency 只需下载 jar 并将其放入您的 Ivy 缓存中。但是您还没有声明您的应用程序依赖于该 jar。 BuildConfig.groovy 有一个 MySQL 驱动程序的注释掉依赖声明。只需取消注释并根据需要更新版本,它就会将其添加到应用程序的类路径中。如果您没有使用install-dependency 这样做,它还会将 jar 下载到您的 Ivy 缓存中。

    【讨论】:

    • 我是不是忘记了之前必须这样做,或者依赖项是 2.0 中的新内容?还是因为我做了install-dependency 而不是丢罐子?
    • 自 1.2 以来一直存在。就像我说的,install-dependency 是一个全局的东西——它只是下载 jar 并缓存它,但对当前的应用程序没有影响。见grails.org/doc/latest/ref/Command%20Line/…
    【解决方案2】:

    我刚刚做了一个 grails>clean 并且成功了

    【讨论】:

    • 它也对我有用。该命令类似于 app_name>grails clean
    • 是的,完全为我工作,无需更改 BuildConfig.groovy
    • 我在将 jar 直接放入 libs 而不是 install-dependency 时遇到了同样的问题。这对我有用
    • 不过,“grails clean”似乎对从 DataSource.groovy 和 BuildConfig.groovy 编译的 *.class 文件没有影响。 (我不知道这些文件是什么时候创建的。)但是使用 groovyc 手动删除和/或重新编译它们是可行的!
    【解决方案3】:

    我最终编写了一个测试与 MySQL 的连接的 groovy 脚本。代码如下。将它放在 scripts/TestMysql.groovy 并运行例如作为

    grails test test-mysql localhost 3306 test-db grails-user grails-password
    

    第一个“测试”是在测试环境中运行脚本,以防DataSource.groovy有任何变化。

    如果您可以使用此脚本连接到 MySQL,那么您至少会知道 DataSource.groovy 的正确设置。如果成功,它还会打印相关的dataSource {} 部分以插入DataSource.groovy

    /* Testing grails/GORM connection to mysql
     * Call this script "scripts/TestMysql.groovy"
     * Usage: "grails test-mysql" or, to run in another environment, e.g. "grails test test-mysql"
     * (This is in case you have changed DataSource.groovy and nothing is working.)
     *
     * Up to five parameters:
     * grails test test-mysql <host> <port> <database> <user> <password>
     * e.g. grails test test-mysql localhost 3306 test root rootpassword.
     * The database name can also be empty.
     */
    
    import groovy.sql.Sql
    
    includeTargets << grailsScript("_GrailsInit") << grailsScript("_GrailsArgParsing")
    
    target(main: "The description of the script goes here!") {
    
        def list=argsMap['params']
        def host=list[0] ? list[0] : 'localhost'  
        def port=list[1] ? list[1] : '3306'       
        def db=list[2] ? list[2] : ''             // can leave empty
        def user=list[3] ? list[3] : 'grails'      
        def pswd=list[4] ? list[4] : 'grails'
    
        println "Connecting to " + host + ":" + port
        println "Database:" + db
        println "User: " + user
        println "Password: " + pswd
    
        def jdbc_string='jdbc:mysql://' + host + ':' + port + '/' + db
    
        def sql
        try {
            sql = Sql.newInstance(jdbc_string, user, pswd, "com.mysql.jdbc.Driver")
        } catch (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException e) {
            println "ERROR! Cannot connect to " + host + ":" + port
            println "Check host, port, your open ports and other firewall settings; try to connect with some other program"
            println ""
            println e
            return
        } catch (com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException e) {
            println "MySQL ERROR, perhaps wrong database name!"
            println ""
            println e
            return
        } catch (java.sql.SQLException e) {
            println "MySQL ERROR, perhaps wrong login/password!"
            println ""
            println e
            return
        }
    
        println "SUCCESS! Connected to MySQL"
        def query = "SHOW DATABASES"
    
        println "Executing query " + query + "..."
        sql.eachRow(query) {
          println it
        }
    
        println "OK, Done!"
    
        println """
            dataSource {
                dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
                url = "jdbc:mysql://${host}:${port}/${db}"
                username="${user}"
                password="${pswd}"
            }
            """
    
    }
    
    setDefaultTarget(main)
    

    更新:确实,一旦这个脚本运行起来,就很容易让 Grails 也运行起来。我只需要做grails clean,删除所有幸存的*.class文件,恢复原来的DataSource.groovy,然后grails compile --refresh-dependencies。然后更改DataSource.groovy,它就像一个魅力。

    这是我的工作DataSource.groovy

    dataSource {
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        username = "sa"
        password = ""
    }
    hibernate {
        cache.use_second_level_cache = true
        cache.use_query_cache = false
        cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
    }
    // environment specific settings
    environments {
        development {
            dataSource {
                dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
                url = "jdbc:mysql://localhost:8890/test"
                username="grails"
                password="grails"
            }
        }
        test {
            dataSource {
                dbCreate = "update"
                url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
                pooled = true
                properties {
                   maxActive = -1
                   minEvictableIdleTimeMillis=1800000
                   timeBetweenEvictionRunsMillis=1800000
                   numTestsPerEvictionRun=3
                   testOnBorrow=true
                   testWhileIdle=true
                   testOnReturn=true
                   validationQuery="SELECT 1"
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多