cancel
Showing results for 
Search instead for 
Did you mean: 

Transactions managed programmatically

sergio13971
Champ in-the-making
Champ in-the-making
Hello everyone, I'm novel in activiti and I wish to manage the execution of services's methods like TaskService (createAttachment(… , setVariables(… ) transactionally, e.g. I want to add some attachments, following execute setVariables(..), but if one of the attachments fail make a rollback of everything saved and discard the execution of setVariables(). It is possible manage this programmatically, starting with a begin() and commit() if everything is ok or rollback in case of failure?

Thanks in advance.

Sergio Mansilla V.
Chile.
4 REPLIES 4

sergio13971
Champ in-the-making
Champ in-the-making
…any idea? I'm very lost in this thing, I've read a lot but unsuccessful. I'm trying to start a transaction from processEngineConfiguration, recovering the TransactionManager and calling begin() method, but it looks like operations disregard this transaction and the activities are processed alone in a different transaction. I'll really appreciate some help!!

Best regards,

martin_grofcik
Confirmed Champ
Confirmed Champ
Hello Sergio.

if
http://www.activiti.org/userguide/#bpmnConcurrencyAndTransactions
is not enough to explain transactions, you can debug some task behavior implementation. When transaction is initiated, committed, rolled back.

Martin

sergio13971
Champ in-the-making
Champ in-the-making
…Martin, thanks for your reply, I read what you recommend me, but, I understand this is about the steps in a workflow, but I want to do is in one transaction add differents attachments to a task, some comments to the same task, and if all of them end successfully update the task, in other case rollback everything done before exception was triggered. All of this made programmatically.

The code here :

    @SuppressWarnings("unchecked")
    public static HashMap<String, Object> updateTaskById(String userId, String taskId, String processInstanceId, HashMap<String, Object> params) throws BussinesException, IOException, SQLException, NotSupportedException, SystemException, NamingException
    {
        HashMap<String, Object> result = new HashMap<String, Object>();
        String consultedUser = (String) params.get("consultedUser");
        WorkflowService.getIdentityService().setAuthenticatedUserId(consultedUser != null && consultedUser.trim().length() > 0 ? consultedUser : userId);
        HashMap<String, String> documentsData = (HashMap<String, String>) params.get("documentsData");
        HashMap<String, Object> variables = (HashMap<String, Object>) params.get("variables");

        System.out.println("VARIABLES=>" + variables);
        String dirPath = null;
        String[] documents = ((String) variables.get("document.documents")).split(";");
        for (String doc : documents)
        {
            String documentPath = documentsData.get(doc + ".path");
            String documentType = documentsData.get(doc + ".contetType");
            String attachmentId;
            if (dirPath == null && documentPath != null && documentPath.length() > 0)
            {
                dirPath = documentPath.substring(0, documentPath.lastIndexOf("/") + 1);
            }

            if (documentPath != null && documentPath.length() > 0)
            {
                if (dirPath == null)
                {
                    dirPath = documentPath.substring(0, documentPath.lastIndexOf("/") + 1);
                }
                File file = new File(documentPath);
                String docKey;
                attachmentId = (String) variables.get(docKey = "document." + doc + ".value");
                if (file.exists())
                {
                    /*If some addAttachment fail, perform a rollback */
                    variables.put(docKey, addAttachment(documentType, taskId, processInstanceId, file.getName(), "", new FileInputStream(file)));
                    if (attachmentId != null && attachmentId.length() > 0)
                    {
                        removeAttachmentById(attachmentId);
                    }
                }
                else
                {
                    throw new BussinesException("No se encontro el Archivo");
                }
            }
        }

        ArrayList<HashMap<String, Object>> comments = (ArrayList<HashMap<String, Object>>) params.get("comments");
        for (HashMap<String, Object> comment : comments)
        {
            String fullMessage = (String) comment.get("fullMessage");
            if (comment.get("id") == null && fullMessage != null)
            {
                System.out.println("Insert Comment=>" + comment);
                /*If some addComment fail, perform a rollback */
                WorkflowService.getTaskService().addComment(taskId, processInstanceId, fullMessage);
            }
        }

        if (dirPath != null)
        {
            FileUtils.cleanDirectory(new File(dirPath));
        }
        updateTaskParams(taskId, variables);
        /*If everything goes fine perform commit */
        result.put("variables", variables);
        System.out.println("VARIABLES=>" + variables);
        return result;
    }

Thanks for your help.

pmsevestre
Champ in-the-making
Champ in-the-making
I think the following forum question can help you:

http://forums.activiti.org/content/external-application-transaction-management

Basically, you need a transaction manager to handle the case where multiple activiti api calls must be handled as a single UoW.