01-10-2013 04:08 PM
I have one list of documents. I create those documents into repository using a "for cycle". I want to know how can I rollback the creation of documents previous in case one error occur. In other words, I want to create all documents or none document.
This is my code:
public String saveFolderAndFiles() throws ClientException {
String message = "";
DocumentModel parentDm = null;
pss = Framework.getService(PathSegmentService.class);
parentDm = tramite.getDocumentParent(nodeParent);
newFolder.setPathInfo(parentDm.getPathAsString(), pss.generatePathSegment(newFolder));
newFolder = documentManager.createDocument(newFolder);
documentManager.saveDocument(newFolder);
//Save files into folder
saveFiles(newFolder, solicitud);
message = "Folder and files saved";
facesMessages.add(StatusMessage.Severity.INFO, message);
return null;
}
protected void saveFiles(DocumentModel newFolder, Solicitud myBean) throws ClientException{
List<File> files = myBean.getFiles();
if(files.size() > 0){
for (File file : files) {
DocumentModel newFile = documentManager.createDocumentModel(file.getType().getId());
newFile.setPropertyValue("file:content", (Serializable) file.getFile());
newFile.setPathInfo(newFolder.getPathAsString(), pss.generatePathSegment(newFile));
newFile = documentManager.createDocument(newFile);
documentManager.saveDocument(newFile);
}
}
}
}
01-14-2013 11:31 AM
What kind of code are you using? Native Nuxeo module? Nuxeo Automation client? CMIS client? It would be best if you showed an excerpt of your code.
01-15-2013 09:06 AM
Hi Florent, I edited my question and I added the code what I use . I hope you can help me.
01-15-2013 09:38 AM
Ok your code runs inside Nuxeo as a plugin.
On error you can just throw an exception and it will bubble up and be detected by the Nuxeo transaction layer which will roll back the current transaction. But this will be seen by the user as a big stacktrace.
If you want to deal with everything yourself, then you should:
The methods for transaction management are quite simple:
TransactionHelper.startTransaction()
TransactionHelper.setTransactionRollbackOnly()
TransactionHelper.commitOrRollbackTransaction()
See the TransactionHelper Javadoc for more.
It's also very important to make sure that you use try
... finally
blocks to ensure that you will always commit or rollback the transaction, and that you will always start a new one when you're done.
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.