Grails 2.4.2: Strange cascading save behaviour in nested transactions

浪尽此生 提交于 2019-12-13 02:32:38

问题


I have some strange behaviour with incorrect cascading of the save in my object graph. Maybe it has something to do with nested transaction, but maybe not.

Here are my 3 domain classes:

Station.groovy

class Station {

  /** Dependency injected {@link de.hutapp.tapandknow.system.SettingsService}.*/
  def settingsService

  static hasMany = [localizedStations: LocalizedStation, 
    stationIdentifiers: AbstractStationIdentifier]

}

LocalizedStation.groovy

class LocalizedStation {

  /** Delete-cascade from the {@link Station} parent. */
  static belongsTo = [station : Station]

  /** An image that represents the LocalizedStation. */
  MediaFile headerImage

  /** The articles inside this LocalizedStation .*/
  List<Article> articles

  static hasMany = [articles: Article]

  static mapping = {
      headerImage cascade: 'all'
      articles cascade: 'all-delete-orphan'
  }
}

MediaFile.groovy

class MediaFile {

  /* some basic properties here*/

  static constraints = {
      name blank: false
      originalFilename blank: false
      description blank: false
  }

  def beforeDelete() {
      // delete the associated file
      new File(getFileLocation()).delete()
  }
}

So the object graph is Station->LocalizedStation->MediaFile

I save/update the objects with the following service methods:

StationService.groovy

@Transactional
class StationService {

def mediaFileService

/**
 * Creates a new {@link Station} and saves it.
 * @return The newly created {@link Station} instance.
 */
Station createStation(LocalizedStation localizedStationInstance, InputStream headerImage, String originalFilename) {

    localizedStationInstance = updateLocalizedStation(localizedStationInstance, headerImage, originalFilename)

    Station stationInstance = new Station()
    stationInstance.addToLocalizedStations(localizedStationInstance)
    stationInstance.save()
    stationInstance.localizedStations[0].headerImage.save() // TODO: check why this is necessary

    return stationInstance
}

LocalizedStation updateLocalizedStation(LocalizedStation localizedStationInstance, InputStream headerSource, String originalFilename) {

    if (headerSource) {
        MediaFile headerFile = mediaFileService.createMediaFile(headerSource, originalFilename, "foobar") // TODO: add description
        localizedStationInstance.headerImage = headerFile
    }

    localizedStationInstance.save()
    return localizedStationInstance

}
}

MediaFileService.groovy

@Transactional
class MediaFileService {

MediaFile createMediaFile(InputStream input, String originalFilename, String description) {

    MediaFile mediaFileInstance = new MediaFile(
            name: originalFilename,
            description: description)

    mediaFileInstance.save(flush: true) // flush for id
    mediaFileInstance.storeMediaFile(input, originalFilename)
    mediaFileInstance.save()

    return mediaFileInstance
}
}

As you can see, if I call stationService.createStation(), I have to manually save the MediaFile, that hangs on the LocalizedStation. This only happens if I call createStation(), which itself calls updateLocalizedStation(). Then the MediaFile won't be persisted unless I save it explicitly in createStation().

If I call updateLocalizedStation() directly, everything works as expected.

Any ideas or suggestions?


UPDATE:

This problems dissappears, if I add @Transactional(propagation=Propagation.REQUIRES_NEW) to the createMediaFile() method.

As far as I understand the documentation, this creates a new transaction. But this is not really what I want, I want a transaction, that spans all methods.

来源:https://stackoverflow.com/questions/24627819/grails-2-4-2-strange-cascading-save-behaviour-in-nested-transactions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!