2012-01-17 16 views
17

Grails的2.0似乎有一些变化DataSource.groovy,我似乎并没有能够让MySQL运行,因为它在1.3.7如何使中,Grails用MySQL 2.0

我做grails install-dependency mysql:mysql-connector-java:5.1.16,而不是仅仅在lib中倾倒.jar。我听说这是最近这样做的方法。

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以有效条目...什么我做错了:

以下是我在DataSource.groovy更换?是否有包含设置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 

回答

21

install-dependency刚刚下载的罐子,把它放在你的常青藤缓存。但是你还没有声明你的应用程序依赖于该jar。 BuildConfig.groovy有一个注释掉的MySQL驱动程序依赖声明。只需取消注释并更新版本,然后将其添加到应用程序的类路径中。如果你还没有用install-dependency这样做,它也会将jar下载到你的Ivy缓存中。

+0

我刚才忘了面前做到这一点,或者是依赖的事情在2.0新功能?或者是因为我做了'install-dependency'而不是丢弃jar? – Mikey 2012-01-17 07:16:54

+2

从1.2开始就一直存在。就像我说的那样,'install-dependency'是一个全局的东西 - 它只是下载jar并缓存它,但对当前应用程序没有影响。请参阅http://grails.org/doc/latest/ref/Command%20Line/install-dependency.html – 2012-01-17 08:01:44

12

我只是做了一个Grails>干净,它的工作

+0

它也适用于我。该命令就像app_name> grails clean – 2012-05-16 15:23:43

+1

我无法给你足够的upvotes。 – 2012-11-21 22:03:28

+0

是我完全为我工作,BuildConfig没有变化。需要groovy – moeTi 2012-12-25 19:53:39

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" 
      } 
     } 
    } 
}