Expand my Community achievements bar.

SOLVED

javax.jcr.InvalidItemStateException: In adding a user to a group

Avatar

Former Community Member

Hi,

I am doing some load tests and i see the below error quite frequently and not sure how i can resolve it. My Sample code is fairly written. I am also using synchronized method just to make sure that no other thread is modifying the group. I am trying to add members to group and i have checked the reference of the method and none of the classes are calling the addMemberToGroup method apart from the one that i am invoking.

Here is the method. I have to mandatory call the save method inside the body because if other threads will modify the definition of group and before the save of current thread then it will result in another issue with jcr.

public synchronized boolean addMemberToGroup(String groupName, String userName, ResourceResolver adminResolver) throws RepositoryException { boolean success=false; if(StringUtils.isNotEmpty(groupName) && StringUtils.isNotEmpty(userName)){ UserManager userManager = adminResolver.adaptTo(UserManager.class); Authorizable userAuthorizable = userManager.getAuthorizable(userName); Authorizable groupAuthorizable = userManager.getAuthorizable(groupName); if (userAuthorizable instanceof User && groupAuthorizable instanceof Group) { Group group = (Group) groupAuthorizable; if (!group.isMember(userAuthorizable)) { group.addMember(userAuthorizable); adminResolver.adaptTo(Session.class).save(); success=true; } } } return success; }

I have attached the exception trace that i am getting. No workflow is running on the groups as well.

at org.apache.jackrabbit.core.ItemSaveOperation.perform(ItemSaveOperation.java:262)
    at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216)
    at org.apache.jackrabbit.core.ItemImpl.perform(ItemImpl.java:91)
    at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:329)
    at org.apache.jackrabbit.core.security.user.UserManagerImpl.setProtectedProperty(UserManagerImpl.java:782)
    at org.apache.jackrabbit.core.security.user.GroupImpl$PropertyBasedMembershipProvider.addMember(GroupImpl.java:420)
    at org.apache.jackrabbit.core.security.user.GroupImpl.addMember(GroupImpl.java:172)
    at aaa.UserGroupOpeartionImpl.addMemberToGroup(UserGroupOpeartionImpl.java:147)

Any help in solving this would be appreciated.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Jörg Hoh wrote...

Hi,

create a new session instead of reusing an existing one, especially when you want to use it in multiple threads. Sessions have never been thread-safe, and in recent versions the devs made it very explicit. In Apache Oak (as part of AEM 6.0) it is highly discouraged (see [1]).

And for the usermanagement related calls you normally don't need to call save();

kind regards,
Jörg

 

[1] http://jackrabbit.apache.org/oak/docs/dos_and_donts.html

 

 

 

Hi, I am currently using 5.6.1. I changed my code to below and still receiving the problem.

public boolean addMemberToGroup(String groupName, String userName) throws LoginException,RepositoryException{ ResourceResolver adminResourceResolver = null; boolean success=false; if(StringUtils.isNotEmpty(groupName) && StringUtils.isNotEmpty(userName)){ try { adminResourceResolver = getResourceResolverFactory().getAdministrativeResourceResolver(null); UserManager userManager = adminResourceResolver.adaptTo(UserManager.class); Authorizable userAuthorizable = userManager.getAuthorizable(userName); Authorizable groupAuthorizable = userManager.getAuthorizable(groupName); if (userAuthorizable instanceof User && groupAuthorizable instanceof Group) { Group group = (Group) groupAuthorizable; if (!group.isMember(userAuthorizable)) { synchronized (this) { group.addMember(userAuthorizable); success=true; } } } } finally { ResourceResolverUtils.closeQuietly(adminResourceResolver); } } return success; }

I think it is fine right now but the error still persists.

View solution in original post

6 Replies

Avatar

Level 10

What is the load you are testing?  Can you set delay & verify?

Thanks,

Sham

Twitter: @adobe_sham

Avatar

Employee Advisor

Hi,

create a new session instead of reusing an existing one, especially when you want to use it in multiple threads. Sessions have never been thread-safe, and in recent versions the devs made it very explicit. In Apache Oak (as part of AEM 6.0) it is highly discouraged (see [1]).

And for the usermanagement related calls you normally don't need to call save();

kind regards,
Jörg

 

[1] http://jackrabbit.apache.org/oak/docs/dos_and_donts.html

Avatar

Level 10

IN AEM 6 - when creating a session, make sure that you use an AEM account that has permissions to add users to groups.  In this example, assume datewrite has the required permissions to perform user/group operations. 

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
 //Create a Session for AEM
 resolver = resolverFactory.getServiceResourceResolver(param);
 session = resolver.adaptTo(Session.class);

 tIn AEM 6 - do not use getAdministrativeResourceResolver() to get a session. To learn how to create an AEM Session via the getServiceResourceResolver() method, see this AEM community article:

 http://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html

You have to use a dedicated AEM user account and perform Sling mapping as discussed in the article. 

Avatar

Correct answer by
Former Community Member

Jörg Hoh wrote...

Hi,

create a new session instead of reusing an existing one, especially when you want to use it in multiple threads. Sessions have never been thread-safe, and in recent versions the devs made it very explicit. In Apache Oak (as part of AEM 6.0) it is highly discouraged (see [1]).

And for the usermanagement related calls you normally don't need to call save();

kind regards,
Jörg

 

[1] http://jackrabbit.apache.org/oak/docs/dos_and_donts.html

 

 

 

Hi, I am currently using 5.6.1. I changed my code to below and still receiving the problem.

public boolean addMemberToGroup(String groupName, String userName) throws LoginException,RepositoryException{ ResourceResolver adminResourceResolver = null; boolean success=false; if(StringUtils.isNotEmpty(groupName) && StringUtils.isNotEmpty(userName)){ try { adminResourceResolver = getResourceResolverFactory().getAdministrativeResourceResolver(null); UserManager userManager = adminResourceResolver.adaptTo(UserManager.class); Authorizable userAuthorizable = userManager.getAuthorizable(userName); Authorizable groupAuthorizable = userManager.getAuthorizable(groupName); if (userAuthorizable instanceof User && groupAuthorizable instanceof Group) { Group group = (Group) groupAuthorizable; if (!group.isMember(userAuthorizable)) { synchronized (this) { group.addMember(userAuthorizable); success=true; } } } } finally { ResourceResolverUtils.closeQuietly(adminResourceResolver); } } return success; }

I think it is fine right now but the error still persists.

Avatar

Former Community Member

Sham HC wrote...

What is the load you are testing?  Can you set delay & verify?

Thanks,

Sham

Twitter: @adobe_sham

 

I had several instance of the same class trying to act upon that method. This was not a osgi service and hence the problem was happening. Once i created a static object in the class and took a lock on that object everything fell into place.

Avatar

Level 10

I will run this code in AEM 5.6 and let you know if it works or if i see the same issue. 

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----