Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: Store session in database [closed]

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

like image 589
Fabe Avatar asked Jan 31 '11 21:01

Fabe


People also ask

How does Tomcat store session data?

By default, the Tomcat instance is configured to store all sessions and their data in memory. Under certain circumstances it may be appropriate to persist the sessions and their data to a repository.

Does Tomcat restore the sessions?

ser and it can be found on temporary directory associated with current context in Tomcat's configuration file. If SESSIONS. ser exists, the doLoad() reads it and tries to restore every found session. The session is represented here as an instance of org.

What is session persistence in Tomcat?

Persistence Across Restarts Whenever Apache Tomcat is shut down normally and restarted, or when an application reload is triggered, the standard Manager implementation will attempt to serialize all currently active sessions to a disk file located via the pathname attribute.

How do I get active sessions in Tomcat?

To find out the number of active sessions, you can use Tomcat's internal statistics that can be accessed using JMX (Java Management Extension). You can also use a JavaEE applications monitoring tool, such as JavaMelody, which helps you monitor Java or Java EE applications in QA and production environments.


2 Answers

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.

like image 65
Bozho Avatar answered Oct 14 '22 09:10

Bozho


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

like image 26
mindas Avatar answered Oct 14 '22 10:10

mindas