Transactions Within Scripting

Calls using the Flex SDK within scripts are not transactional. This is unlike the legacy scripting approach, but is unavoidable now that Flex is a distributed microservices-based system. This means that is the responsibility of the script developer to anticipate failures and to perform any clear up operations in case of failure.

NOTE

If there are steps that you must perform in a single transaction, please contact your Flex representative to discuss your requirements.

Example

The JEF Script action example below creates new media asset of variant v1, the variant having a default media asset metadata definition that does not have a field named field-that-does-not-exist):

def execute() {
 
  def assetService = flexSdkClient.getAssetService();
  
  NewAssetPlaceholder assetPh = NewAssetPlaceholder.builder()
        .type("media-asset")
        .name("demo asset")
        .variant("v1")
        .build();
  
  def asset = assetService.createAsset(assetPh);
   
  def metadata = assetService.getAssetMetadata(asset.getId());
  def field = metadata.getField('field-that-does-not-exist')

}

If a job of this type is executed, it will fail (with a tv.nativ.mio.commons.MioException: FlexSdkException: No field exists with name: string-fiedld error), but the new demo asset media asset will exist. This is different to the legacy scripting framework whereby the exception thrown when trying to retrieve the non-existing field would cause a transaction rollback (and hence the asset would be removed from the underlying database).

In the updated example below, the script now catches any exceptions and destroys the created asset:

def execute() {
  
  def assetService = flexSdkClient.getAssetService();
  
  NewAssetPlaceholder assetPh = NewAssetPlaceholder.builder()
        .type("media-asset")
        .name("demo asset")
        .variant("v1")
        .build();
  
  def asset = assetService.createAsset(assetPh);
   
  try {
  
    def metadata = assetService.getAssetMetadata(asset.getId());
    def field = metadata.getField('field-that-does-not-exist')
  
  } catch (Exception ex) {
    assetService.destroy(asset.id);
    throw ex;
  } 
    
}

Note that the exception is re-thrown in order to ensure that the job fails.

The choice of what and how to rollback is up to the script developer. If no manual rollbacks are performed, please be aware that the system maybe left in a logically inconsistent state.