Search code examples
grailsgrails-ormbatch-processing

Grails, Batch inserting failed when there is an exception of one single insert


I'm trying to do a batch insertion using files retrieved from a multiple input. It's all good when there is no exception. But when there is one single exception caused by one file with bad content, all the others are not persisted. Here below the code i used:

 def fileList = multiPartRequest.getFiles('files')
    def totalFile = fileList?.size();
    fileList.eachWithIndex { file,int i ->

            try{
                def result=urlService.loadFile(file.getInputStream() as InputStream)
                urlService.newUrlFromFile(params,result)

            } catch (Exception e){
                def errorFile = file.getOriginalFilename()
                log.warn("Problem with file: $errorFile ",e)
                errorsFiles.add(errorFile)                    
            }             


    }

and the newUrlFromFile()method:

def newUrlFromFile(def params,Map result){
    def datasetId = params?.dataset_id
    def categories = result?.info as List
    def catMap =[:]
    Url url;

    def dataset = DataSet.findById(datasetId)
    if (datasetId){
        if(categories){

            catMap=[level1:categories[0],level2:categories[1],level3:categories[2]]
            url=CSVLoaderService.saveUrlAndCategories(categories[3] as String,categories[0] as String,
                    categories[1] as String,categories[2] as String,dataset)
            url.save(flush: true,failOnError: true)
            dataset.save(flush: true, failOnError: true,deepValidate: false)
            log.info("we have saved the categories")
        }

        Map doc =[dataset_id:datasetId,
                  url_id:url?.id as String,
                  meta_info:result?.meta,
                  title: result?.title,
                  categories:catMap,
                  dataset_lang:dataset.language]
      }
}

Thanks


Solution

  • Finally, I can resolve using :

                    try{
                        def result=urlService.loadFile(file.getInputStream() as InputStream)
                        DataSet.withNewTransaction {
                        urlService.newUrlFromFile(params,result)
                        }
    
                    } catch (Exception e){
                        def errorFile = file.getOriginalFilename()
                        log.warn("Problem with file: $errorFile ",e)
                        errorsFiles.add(errorFile)
                        flash.error="Problem encountered with file: ${file.getOriginalFilename()}";
                    }
    

    This will open a new traction each time encounter a new file.