初学者面临Grails的困难

时间:2014-10-27 07:32:16

标签: grails

我是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

2 个答案:

答案 0 :(得分:1)

问题出在这一行:

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

答案 1 :(得分:-1)

新手。 欢迎使用Grails

你的问题如下:你必须采取的措施

您看到的错误来自不同的原因:

  

transactionManager,sessionfactroy

<强> 1 即可。请确保您先运行MySQL实例,然后再运行端口。

2. 检查密码和用户名是否正确,并且可以连接到mysql控制台。

3. 确保(UserProd)MySQL数据库存在,

检查此设置是否有缓存,如果有任何改变,请将其恢复?

  

cache.region.factory_class =&#39; org.hibernate.cache.ehcache.EhCacheRegionFactory&#39; // Hibernate 4