If you have enabled sessions for your GAE application, you probably already know that sessions are persisted in datastore. But, do you know that expired sessions are not deleted automatically? If you don’t take care of expired session, they’ll slowly eat your storage quota.
Fortunately, Google has provided a Java servlet that deletes expires session (although 100 per invocation) but for some strange reason this is not documented. Here’s what do you need to do…
Add SessionCleanupServlet to your web.xml:
...
<servlet>
<servlet-name>_ah_sessioncleanup</servlet-name>
<servlet-class>com.google.apphosting.utils.servlet.SessionCleanupServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>_ah_sessioncleanup</servlet-name>
<url-pattern>/_ah/sessioncleanup</url-pattern>
</servlet-mapping>
...
<security-constraint>
<web-resource-collection>
...
<url-pattern>/_ah/sessioncleanup</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
Use cron.xml to schedule periodic cleanup:
<cronentries>
...
<cron>
<url>/_ah/sessioncleanup?clear</url>
<description>Clean up sessions</description>
<schedule>every 15 minutes</schedule>
</cron>
</cronentries>
Considering that the servlet deletes maximum of 100 session per call, you should set an appropriate cleanup schedule for your application to make sure sessions do not pile up.
Using Java? Speed-up bug fixing and improve customer satisfaction using LogDigger to create detailed error reports and notifications for your web application
{ 2 comments… read them below or add one }
I set my web.xml and cron.xml using your code.
When I see the Current Load view area in my appengine dashboard , it works well.
But my _ah_SESSION table data still remains.
I used setMaxInactiveInterval(60*20) when client logins.
Is there more anything else what I have to?
Note that the sessioncleanup servlet deletes only 100 entries per invocation. Make sure that cron scheduling is appropriate so that it can delete outdated sessions faster than new are created. If you have many expired sessions it may take some time before you notice a difference in datastore statistics (which is updated daily). Hope this helps.