Grails,当单个插入出现异常时,批量插入失败
Grails, Batch inserting failed when there is an exception of one single insert
我正在尝试使用从多输入中检索到的文件进行批量插入。没有例外的时候一切都很好。但是当一个文件内容不好导致一个异常时,其他的都不会被持久化。下面是我使用的代码:
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]
}
}
谢谢
最后,我可以解决使用:
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()}";
}
每次遇到新文件都会打开一个新的牵引力。
我正在尝试使用从多输入中检索到的文件进行批量插入。没有例外的时候一切都很好。但是当一个文件内容不好导致一个异常时,其他的都不会被持久化。下面是我使用的代码:
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]
}
}
谢谢
最后,我可以解决使用:
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()}";
}
每次遇到新文件都会打开一个新的牵引力。