2012-01-17 25 views
17

Grails 2.0 dường như có một số thay đổi để DataSource.groovy và tôi dường như không có khả năng để có được mysql chạy như trong 1.3.7Làm thế nào để làm cho MySQL làm việc trên grails 2.0

tôi đã làm grails install-dependency mysql:mysql-connector-java:5.1.16 thay vì chỉ bán phá giá .jar trong lib. Tôi nghe đây là cách để làm điều đó những ngày này.

Dưới đây là những gì tôi đã thay thế trong tôi DataSource.groovy:

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

Với

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

Thay đổi tất nhiên tên người dùng, mật khẩu và dbNamer đến mục hợp lệ ... Tôi đang làm gì sai? Có một grails 2.0 hướng dẫn bao gồm thiết lập mysql?

tôi nhận được lỗi con quái vật này:

| 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 

Trả lời

21

install-dependency chỉ tải các lọ và đặt nó trong bộ nhớ cache Ivy của bạn. Nhưng bạn chưa tuyên bố rằng ứng dụng của bạn có sự phụ thuộc vào cái bình đó. BuildConfig.groovy có tuyên bố phụ thuộc nhận xét cho trình điều khiển MySQL. Chỉ cần bỏ ghi chú đó và cập nhật phiên bản nếu bạn muốn và nó sẽ thêm nó vào classpath của ứng dụng của bạn. Nó cũng sẽ tải jar vào bộ nhớ cache Ivy của bạn nếu bạn không làm điều đó với install-dependency.

+0

Ý của tôi chỉ cần quên đi việc phải làm điều này trước đây, hoặc là điều phụ thuộc mới trong 2.0? hay là vì tôi đã cài đặt 'phụ thuộc' thay vì thả cái bình? – Mikey

+2

Đã có khoảng từ 1.2. Như tôi đã nói, 'install-dependency' là một thứ toàn cầu - nó chỉ tải jar và lưu trữ nó, nhưng không ảnh hưởng đến ứng dụng hiện tại. Xem http://grails.org/doc/latest/ref/Command%20Line/install-dependency.html –

12

Tôi chỉ làm một grails> sạch và nó hoạt động

+0

nó cũng làm việc cho tôi. lệnh giống như app_name> grails clean –

+1

Tôi không thể cho bạn đủ upvotes. –

+0

yep hoàn toàn phù hợp với tôi, không có thay đổi trong BuildConfig.groovy yêu cầu – moeTi

3

Tôi đã kết thúc bằng văn bản một tập lệnh groovy để kiểm tra kết nối với MySQL. Đoạn mã sau. Đặt nó trong tập lệnh/TestMysql.groovy và chạy ví dụ: như

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

Thử nghiệm đầu tiên là chạy tập lệnh trong môi trường thử nghiệm, trong trường hợp có bất kỳ thay đổi nào trong DataSource.groovy.

Nếu bạn có thể kết nối với MySQL bằng tập lệnh này, ít nhất bạn sẽ biết cài đặt chính xác cho DataSource.groovy của mình. Trong trường hợp thành công, nó cũng in phần dataSource {} có liên quan để chèn vào 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) 

Cập nhật: thật vậy, một khi kịch bản này đã được làm việc, đó là dễ dàng để làm Grails làm việc là tốt. Tôi chỉ cần làm grails clean, xóa tất cả *.class tệp đã tồn tại, khôi phục DataSource.groovy gốc và sau đó grails compile --refresh-dependencies. Sau đó thay đổi DataSource.groovy và nó hoạt động như một sự quyến rũ.

Sau đây là tôi DataSource.groovy làm việc:

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" 
      } 
     } 
    } 
} 
Các vấn đề liên quan