Grails“在发生异常后不会刷新会话”错误消息

时间:2012-09-09 19:48:15

标签: grails groovy grails-domain-class

我看到这个错误消息已经在hibernate的上下文中多次发布了。

我在使用grails服务和域类时遇到此错误,任何帮助都将非常感激

域类

class Coupon {
    Date dateCreated
    Date lastUpdated
    String code
    String email
    String address
    String state
    String city
    String zip
    def couponCodeGeneratorService

    def beforeValidate() {
        println code+"---------8-"
        code = couponCodeGeneratorService.generate()
        println code+"----------"
    }
    static constraints = {
        email blank:false,email:true
        address blank:false
        state blank:false
        city blank:false
        zip blank:false
    }
}

服务

class CouponCodeGeneratorService {
    Random randomGenerator = new Random()
    def serviceMethod() {

    }
    def generate(){
        def group1 =  randomGenerator.nextInt(9999)+"";
        def group2 =  randomGenerator.nextInt(9999)+"";
        def group3 =  randomGenerator.nextInt(9999)+"";
        def group4 =  randomGenerator.nextInt(9999)+"";
        return group1.padLeft(4,"0") +group2.padLeft(4,"0")+group3.padLeft(4,"0")+group4.padLeft(4,"0")
    }
}

我得到的错误是

---------8-
4844634041715590----------
4844634041715590---------8-
| Error 2012-09-10 11:32:54,938 [http-bio-8080-exec-7] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
   Line | Method
->>  19 | beforeValidate     in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
|    24 | save . . . . . . . in com.easytha.CouponController
|   186 | doFilter           in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|   886 | runTask            in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . . . . . .  in     ''
^   662 | run                in java.lang.Thread
| Error 2012-09-10 11:32:54,944 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  - AssertionFailure occurred when processing request: [POST] /EasyTha/coupon/save - parameters:
zip: asdf
address: asd
email: s.s@s.xom
state: asd
code: 
create: Create
city: asdf
null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
   Line | Method
->>  19 | beforeValidate     in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
|    24 | save . . . . . . . in com.easytha.CouponController
|   186 | doFilter           in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|   886 | runTask            in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . . . . . .  in     ''
^   662 | run                in java.lang.Thread

我对Hibernate不是很熟悉,这也是创建看起来像信用卡号的优惠券代码的正确方法吗?

1 个答案:

答案 0 :(得分:6)

我怀疑问题可能是CouponCodeGeneratorService是事务性的。因此,当您从beforeValidate内部调用服务方法时,您需要打开和关闭一个事务(即使您没有触及该方法中的数据库),这将导致另一个冲洗会议。

尝试使服务非交易:

static transactional = false