我正在尝试使用从多个输入中检索的文件进行批量插入。没有例外,这一切都很好。但是,当一个文件内容不正确导致一个例外时,其他所有文件都不会保留。下面是我使用的代码:
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)
}
}
和newUrlFromFile()
方法:
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]
}
}
由于
答案 0 :(得分:0)
最后,我可以解决使用:
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()}";
}
每次遇到新文件时都会打开一个新的牵引力。