How Can I manage transactions on creation of documents?

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);

        //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);


1 votes

1 answers



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.

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

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 tryfinally 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.

1 votes