cancel
Showing results for 
Search instead for 
Did you mean: 

How Can I manage transactions on creation of documents?

geekonspace
Star Contributor
Star Contributor

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);
                        }
                }
                
        }
        
}
3 REPLIES 3

Florent_Guillau
World-Class Innovator
World-Class Innovator

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.

geekonspace
Star Contributor
Star Contributor

Hi Florent, I edited my question and I added the code what I use . I hope you can help me.

Florent_Guillau
World-Class Innovator
World-Class Innovator

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:

  • At the beginning commit the existing transaction to commit things that could have been done before you entered your code.
  • Start a transaction that you'll manage yourself
  • During your work if there's an error then mark the transaction as rollback only and stop doing your work.
  • After your work commit/rollback the transaction.
  • At the end start a new transaction because the callers will expect one to be present.

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.