Let's say I have a counter function which updates a counter using raw SQL:
public void updateCounter() {
executeSql("UPDATE counter SET count_value = count_value + 1 WHERE id = 1;");
}
The database would make sure that two concurrent calls to the counter are handled as expected - that all calls would update the counter with one increment and no updates would get lost.
Rather than executing this by issuing a raw SQL command I'd like to use GORM. The naïve way to do it would be something along the lines of:
public void updateCounter() {
Counter c = Counter.get(1)
c.countValue += 1
c.save()
}
In this case I'd assume that an update could be lost if two threads call the updateCounter() method at the same moment. What is the correct "Grails/GORM-way" to handle this concurrency issue?