spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

improve HazelcastSessionRepository connection exception handling

Open michaelhaessig opened this issue 6 years ago • 2 comments

The current implementation of HazelcastSessionRepository does not handle any HazelcastException when accessing the sessions map.

In the case of a hazelcast server connection lost, this causes all requests to return 500 error status.

The reason for this is the SessionRepositoryFilter calls the HazelcastSessionRepository early in the request cycle which throws an Exception when accessing the sessions IMap.

Since the HazelcastSession is only visible in the package i had to completely replace the current HazelcastSessionRepository with my own implementation to add additional exception handling.

Example code for Hazelcast Exception Handling:

 @Override
    public HazelcastSession findById(String id) {

        try {
            MapSession saved = this.sessions.get(id);
            if (saved == null) {
                return null;
            }
            if (saved.isExpired()) {
                deleteById(saved.getId());
                return null;
            }
            return new HazelcastSession(saved);
        } catch(HazelcastException e){
            LOG.error("hazelcast session failed to find", e);

            return null;
        }
    }

I've added a try catch around every this.sessions access. I'm not sure if this is the right approach i would appreciate any better suggestions to prevent this situation. Thanks in advance.

michaelhaessig avatar Jan 18 '19 15:01 michaelhaessig

Thanks for the report @michaelhaessig.

We'll need to take a look at this one from a higher level, and see how other session stores behave in the similar situation.

vpavic avatar Jan 26 '19 14:01 vpavic

makes sense if this could be handled in the SessionRepositoryFilter so all SessionRepository implementations can benefit.

michaelhaessig avatar Jan 28 '19 06:01 michaelhaessig