gpt4 book ai didi

grails - 初学者在Grails中面临困难

转载 作者:行者123 更新时间:2023-12-02 15:26:32 25 4
gpt4 key购买 nike

我是Grails的新手。要连接数据库,只需更改用户名和密码,然后在dataSource.groovy文件中添加表名即可。代码如下:

dataSource {
pooled = true
jmxExport = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQLDialect"
username = "root"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
// cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}

// environment specific settings
environments {
development {
dataSource {
url = "jdbc:mysql://localhost/user"
username = "root"
password = ""
}
}
test {
dataSource {
url = "jdbc:mysql://localhost/user_prod"
username = "root"
password = ""
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/user_prod"
username = "root"
password = ""
}
}
}

单击运行方式按钮后,遇到以下错误:
    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
^ 722 | 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': Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
->> 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
^ 722 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
->> 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
^ 722 | run . . . in java.lang.Thread
Caused by HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
->> 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
^ 722 | run . . . in java.lang.Thread
Caused by ClassNotFoundException: org.hibernate.cache.ehcache.EhCacheRegionFactory
->> 156 | findClass in org.codehaus.groovy.tools.RootLoader
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 423 | loadClass in java.lang.ClassLoader
| 128 | loadClass in org.codehaus.groovy.tools.RootLoader
| 356 | loadClass in java.lang.ClassLoader
| 186 | forName . in java.lang.Class
| 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
^ 722 | run in java.lang.Thread

最佳答案

问题在这一行:

Caused by ClassNotFoundException: org.hibernate.cache.ehcache.EhCacheRegionFactory

当Grails繁荣发展时,您往往会获得相当大的堆栈跟踪信息,这可能掩盖了真正的问题。如此大的堆栈可能仅占实际堆栈的10-20%,因为默认情况下会排除许多明显无用的堆栈帧。但总的来说,您经常会看到一连串的异常,但导致当前异常的异常显示如下。因此,最好从头开始阅读,因为这通常是核心问题。

我不确定为什么缺少ehcache依赖项-您是否在BuildConfig.groovy中将其排除在外?您可以添加一个依赖项:
dependencies {
...
compile 'net.sf.ehcache:ehcache-core:2.4.8'
}

看看是否有帮助。

编辑

实际上不,这不是问题,因为它引用的是与Ehcache一起使用的Hibernate类,而不是Ehcache类。因此,这是一个Hibernate v3 / v4问题。 Grails现在默认为Hibernate 4.x,但包括使用Hibernate 3.x的配置设置。您显然在BuildConfig.groovy中切换到了Hibernate 3,但是没有更新 DataSource.groovy中的数据源配置-注释掉
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4

并改用它:
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3

关于grails - 初学者在Grails中面临困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com