We are seeing this error in the logs. I've seen that the vendor who wrote code for us is using something like this
ResourceResolver resourceResolver = request.getResourceResolver();
QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Session session = resourceResolver.adaptTo(Session.class);
Does this session need to be closed in finally as well os it's only when we try to get administrative session that we need to correctly close it?
Any links for best practices while writing servlets and using sessions would be appreciated.
Solved! Go to Solution.
Views
Replies
Total Likes
No, rule is: You create it, you close it. Does not apply for adaptTo()
Views
Replies
Total Likes
We need to close the ResourceResolver or the Session which we initiate. Refer [1], [2] in to know more in detail
[2] http://www.wemblog.com/2014/08/how-to-use-sessions-and-resource.html
Views
Replies
Total Likes
Does this apply only while getting an admin session using resolverfactory or even a regular session using resourceResolver.adaptTo(Session.class) using the request object of the servlet?
Views
Replies
Total Likes
Or it makes sense to use every component like this
@Activate
protected void activate() {
session = null;
try {
session = repo.loginAdministrative();
…
} catch (RepositoryException e) {
// log the exception
}
}
@Deactivate
protected void deactivate() {
if (session != null && session.isLive()) {
session.logout();
}
}
Views
Replies
Total Likes
We should not be using loginAdministrative()
when you are using resourceResolver.adaptTo(Session.class)
make sure you close resourceResolver in the final block
Views
Replies
Total Likes
No I meant either ways, not that we are opening sessions using both methods. If I just use session using AdaptTo, does that also need to be closed using session.close()
Views
Replies
Total Likes
No, rule is: You create it, you close it. Does not apply for adaptTo()
Views
Replies
Total Likes
Yes.. You will have to close the session by session.logout()
Views
Replies
Total Likes
hey per ~~Feike Visser if I'm using adaptTo I don't need to close it
Views
Replies
Total Likes
Either you need to do Session.logout() just to close the session and not the resource, If you are doing ResourceResolver.close() then you dont have to explicitly do session logout.
Views
Replies
Total Likes
Views
Replies
Total Likes
Here is a good article on this:
http://scottsdigitalcommunity.blogspot.ca/2014/12/querying-adobe-experience-manager-6.html
Yes - close session by invoking session.logout();
Views
Replies
Total Likes