tags:

views:

42

answers:

1

My domain class is like this:

Class Account {
    String accountNo
    ...
    def beforeUpdate = {
        new AuditTrial(eventName:"update").save()
    }
}

In my application there is a block-level transaction as follows:

def updateAccount = {
    Account.withTransaction { status ->
        def source = Account.get(params.from)
        def dest = Account.get(params.to)

        def amount = params.amount.toInteger() 
        if(source.active) { 
            source.balance -= amount 
            if(dest.active) {
                dest.amount += amount
            } else { 
                status.setRollbackOnly() 
            } 
        } 
    }
}

When I try to invoke this updateAccount method, it gives a stack overflow exception. It seems to be that the beforeUpdate method has been called recursively.

expect your worth suggestions to overcome this problem

A: 

Does AuditTrail have a cascading relationship to Account? That could be causing the problem.

The other thing that could be happening is that saving the AuditTrail is causing a session flush which also saves the modified Account objects.

Have you tried using afterInsert instead?

leebutts
afterInsert is probably the better place to do the work. You'll also want to run it in a new Hibernate session: `Account.withNewSession { session -> new AuditTrial(eventName:"update").save() }`
Burt Beckwith