2012-01-17 46 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" 
      } 
     } 
    } 
}