Need help with resourceResolver.commit() | Community
Skip to main content
Level 6
June 21, 2022
Solved

Need help with resourceResolver.commit()

  • June 21, 2022
  • 5 replies
  • 2798 views

Hi All

I need help with resourceResolver.commit(). When I do that I get org.apache.sling.api.resource.PersistenceException: Unable to commit changes to session.

 

This is the code that is generating the PersistanceException:

 

logger.info("\n EventZZZ component path: {}", component);
    resourceResolverService.withServiceUserResourceResolver(ServiceUser.CONTENT_READER, resourceResolver -> {
        Resource resource = resourceResolver.getResource(component);
        resetCampaign(resource);
        try {
            if (resourceResolver.hasChanges()) {
                resourceResolver.commit();
            }
        } catch (PersistenceException e) {
            logger.error(String.valueOf(e));
        }
    });

 

The resourceResolver.hasChanges() is true and I am expecting resourceResolver.commit() would persist the node change in repo.

 

private void resetCampaign(Resource res) {
Node node = res.adaptTo(Node.class);

try {

if (node.hasProperty("campaign")) {
node.setProperty("campaign", StringUtils.EMPTY);
logger.info("\n Removed property CAMPAIGN");
}
if (node.hasProperty("campaignTargetGroup")) {
node.setProperty("campaignTargetGroup", StringUtils.EMPTY);
logger.info("\n Removed property campaignTargetGroup");
}
if (node.hasProperty("campaignTopic")) {
node.setProperty("campaignTopic", StringUtils.EMPTY);
logger.info("\n Removed property campaignTopic");
}
node.setProperty("eventHandlerTask", "By "+ res.getResourceResolver().getUserID());



} catch (RepositoryException e) {
logger.error(String.valueOf(e));
}

}

 

I followed all several Q/A like this one and this one.

Can you please point me what is wrong with this approach?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

This could be due to permission, check the modify and create permission to the campaign for the service user.

5 replies

JeevanRaj
Community Advisor
Community Advisor
June 21, 2022

Hi @anasustic 

 

The service user which you are using to get the resource resolver might not have "modify" access to the resource you are trying to modify. Please check the permissions for this service user.

ShaileshBassi
Community Advisor
Community Advisor
June 21, 2022

@anasustic 

Instead of adapting the resource to node, use

 ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
  if (properties.containsKey("campaign")) {
    properties.put("campaign", StringUtils.EMPTY);
  } 
 if  (properties.containsKey("campaignTargetGroup")) {
    properties.put("campaignTargetGroup", StringUtils.EMPTY);
  }  and so on.

and if you want to go with the node

Session session = resourceResolver.adapTo(Session.class)
session.save()
As directed in the Reference link :https://aem4beginner.blogspot.com/update-node-property-using-aem-bundle

Thanks

anasusticAuthor
Level 6
June 21, 2022

Thanks @shaileshbassi for the advice.

I modifed the method accordingly.

private void resetCampaign(Resource res) {
    ModifiableValueMap properties = res.adaptTo(ModifiableValueMap.class);
    if (properties.containsKey("campaign")) {
        properties.put("campaign", StringUtils.EMPTY);
        logger.info("\n Removed property campaign");
    }
    if  (properties.containsKey("campaignTargetGroup")) {
        properties.put("campaignTargetGroup", StringUtils.EMPTY);
        logger.info("\n Removed property campaignTargetGroup");
    }
    if (properties.containsKey("campaignTopic")) {
        properties.put("campaignTopic", StringUtils.EMPTY);
        logger.info("\n Removed property campaignTopic");
    }
}
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
June 21, 2022

This could be due to permission, check the modify and create permission to the campaign for the service user.

Arun Patidar
ArpitVarshney
Community Advisor
Community Advisor
June 21, 2022

Hey @anasustic 

@arunpatidar is right that it seems to be a permission issue. When I see your code and noticed that you are using the CONTENT_READER service user to write the data and I don't think the CONTENT_READER service user is intended to write something in the repository so you need to validate the permission of your service user.

From my perspective, You should either use an existing service user who has permission to write in the repository or create a new system user and use that one. 

Regards,

Arpit Varshney

anasusticAuthor
Level 6
June 21, 2022

Thanks so much @arpitvarshney @arunpatidar @shaileshbassi @jeevanraj for your replies.

I created a new System User system-content-writer and gave it write permissions to specific paths. 

 

I then used this new created account in my code for the commit()

resourceResolverService.withServiceUserResourceResolver(ServiceUser.CONTENT_WRITER, resourceResolver -> {
Resource resource = resourceResolver.getResource(component);
resetCampaign(resource);
try {
if (resourceResolver.hasChanges()) {
resourceResolver.commit();
}
} catch (PersistenceException e) {
logger.error(e.getMessage() + "\n" + e);

}
});