8

I am searching for a way to avoid in-memory session replication/clustering and store the session in a database. Using Tomcat's JDBCStore is useless at this point, because it only stores inactive sessions in the database to save the servers memory. Any suggestions?

Thanks upfront Fabian

1
  • 2
    Out of curiosity, have you looked at Elastic Beanstalk from Amazon? They provide sticky sessions.
    – stepanian
    Sep 9, 2011 at 4:46

3 Answers 3

7

If you don't want to use the session in the way it is supposed to be used, then don't use it at all - develop your own session object. It can still implement HttpSession, and even extend from an implementation of HttpSession.

You can use a Filter to wrap your request so that it returns your session object rather than the standard one. In your session you can store things in DB instead of in-memory.

Instead of writing to the DB, you can use Hazelcast - it provides distributed collections. But I guess it will take the same amount of effort as to configure session replication. And session replication is not something that hard - it is supported by all containers.

These are rough guidelines, the task will not be trivial. And I'd recommend sticking to the standard session usage patterns, and storing things in DB only if really needed.

In order to avoid the need of replication you might try using sticky sessions - i.e. when a user is directed to a server by the load balancer, each subsequent request by that user is sent to the same server.

2
  • Thanks for the quick response. My intention is to run several Tomcat's in Microsoft Azure. The problem with sticky-sessions is, that it is not supported by Azure's load balancer. The easiest way to achieve session replication in this cloud environment is to store the session in the db. Do you have any code samples for the HttpSession extension and the Filter? Thanks for your answer
    – Fabe
    Jan 31, 2011 at 21:43
  • @Fabe - I don't have examples, sorry.
    – Bozho
    Jan 31, 2011 at 21:48
3

You might want to look at this project, I would rather prefer storing sessions in memcached rather than in the DB.

3
  • I checket it out - very interesting, but the non-sticky Session Version is Not Ready for production ;(
    – Fabe
    Feb 1, 2011 at 5:53
  • interesting project indeed, +1
    – Bozho
    Feb 1, 2011 at 7:24
  • 1
    btw, memcached-session-manager 1.4 is now released - with support for non-sticky sessions Mar 9, 2011 at 9:47
0

Seems to me there is no way then implementing an JDBC Session Manager on my own, but thanks for your answers and time - Hazelcast needs some closer look, seems very powerful for me. I will do something similar like J. Brisbin: http://jbrisbin.com/web2/articles/tomcat-session-manager-backed-by-riak/

Not the answer you're looking for? Browse other questions tagged or ask your own question.