Grails2.0ではDataSource.groovy
にいくつかの変更が加えられているようで、1.3.7のようにmysqlを実行できないようです。
.jarをlibにダンプするだけでなく、grails install-dependency mysql:mysql-connector-Java:5.1.16
を実行しました。これが最近のやり方だと聞いています。
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のセットアップをカバーするgrails2.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
install-dependency
は、jarをダウンロードして、Ivyキャッシュに配置するだけです。しかし、アプリケーションがそのjarに依存していることを宣言していません。 BuildConfig.groovy
には、MySQLドライバーのコメントアウトされた依存関係宣言があります。コメントを外し、必要に応じてバージョンを更新すると、アプリケーションのクラスパスに追加されます。また、install-dependency
で実行していなかった場合は、jarをIvyキャッシュにダウンロードします。
Grails> Cleanを実行したところ、機能しました
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"
}
}
}
}