(Quick Reference)

5 Transactions - Reference Documentation

Authors: Graeme Rocher

Version: 5.0.0.BUILD-SNAPSHOT

5 Transactions

Transactions in Redis (using MULTI/EXEC) operate differently to SQL transactions. In fact they are more a way to do bulk operations that can be discarded rather than full transactions (see the documentation on MULTI/EXEC for further information).

One limitation of Redis' MULT/EXEC command is that even reads are batched up in the transaction. This trickles down to usage within GORM for Redis. So for example you can execute a transaction such as:

Person.withTransaction {
    new Person(firstName:"Bob").save()
    new Person(firstName:"Fred").save()
}

However, you cannot execute any queries (only write operations) in the middle of a transaction unless you do so with a separate connection such as:

Person.withTransaction {
    new Person(firstName:"Bob").save()

def fred Person.withNewSession { fred = Person.findByFirstName("Fred") } if (!fred) { new Person(firstName:"Fred").save() } }

In other words all read operations have to happen in a separate session/connection which can be achieved with the withNewSession method.