Reputation: 1576
Sometimes in Grails Service I need to handle many transaction among two to three tables:
class MyService {
void saveOwner(Map params) {
...
params.get("schedules").each { saveSchedule(it) }
}
void saveSchedule(Owner owner, Map schedule) {
Schedule s
if(schedule.get("id") != null) {
s = Schedule.createCriteria().get {
eq("owner", owner)
eq("schedule", schedule.get("seqNo"))
}
}
if(s == null) {
s = new Schedule()
schedule.put("seqNo", generateSeqNo(owner))
}
s.setProperties(schedule)
s.save(faileOnError: true)
}
Long generateSeqNo(Owner owner) {
Long seqNo = Schedule.createCriteria().get {
projections {
max("seqNo")
}
eq("owner", owner)
}
if(seqNo == null) { seqNo = 0 }
return ++seqNo
}
}
Now in the method generateSeqNo()
always returns the same sequence number. It seems that Hibernate doesn't include by default Domain instances that are not yet flushed on the database. How can I set Grails to include this persisted data only to the queries inside this service?
Upvotes: 0
Views: 52
Reputation: 508
You can mark the service as non-transactional to get over this problem:
class MyService {
static transactional = false
...
}
You can always make single operations inside transaction if needed.
Upvotes: 1