class ExtHotelApiService extends HotelApiService {
static scope = "singleton"
static transactional = true
def save(params) {
params.hotels.each{ht->
try{
transactionalSave(ht)
} catch(Exception e) {
/* exceptions handling */
}
}
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollBackFor=RollBackError.class)
def transactionalSave(ht) throws RollBackError {
/* saving hotel and hotel description */
}
}
Note few things:
- ExtHotelApiService extends HotelApiService
- RollBackError extends RuntimeException
- ExtHotelApiService.transactional = true
- HotelApiService.transactional = false
- We must save hotel and only then save the description
- We must roll back transaction (and not persist hotel) if description is not valid (can't be persisted)
All code was wrote according to Declarative Transactions guide but there is one trouble - no roll back happens at all! :(
Transaction successfully commits and hotel is persisted into DB even after RollBackError is thrown!
Where i have make mistake and how to work with declarative transactions in right way ?