Sap
Sap

Reputation: 5321

Grails "don't flush the Session after an exception occurs" error message

I see that this error message has been posted several times already in the context of hibernate.

I am getting this error while using grails service and a domain class, any help will be really appreciated

Domain class

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
    }
}

Service

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")
    }
}

The error I am getting is

---------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: [email protected]
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

I am not very familiar with Hibernate, also is this a correct way to create a coupon code that looks like a credit card number?

Upvotes: 1

Views: 6519

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122424

I suspect the problem may be that the CouponCodeGeneratorService is transactional. Therefore, when you call the service method from inside your beforeValidate you're opening and closing a transaction (even though you don't touch the database inside the method), which among other things will cause another flush of the session.

Try making the service non-transactional:

static transactional = false

Upvotes: 6

Related Questions