spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.12k forks source link

Session scoped beans PreDestroy not called #1256

Open lRawd opened 5 years ago

lRawd commented 5 years ago

Spring Session 2.1.1.RELEASE This (https://spring.io/blog/2018/10/31/spring-session-bean-ga-released) mentions that it is now possible to use PreDestroy with Session scoped beans. I do not see the valueUnbound method being called when a Session is destroyed, and therefor PreDestroy is never called.

vpavic commented 5 years ago

Thanks for the report @lRawd.

It's seems that our HttpSessionBindingListener support is actually missing a key piece that's required for @PreDestroy, and that's removal of attributes on expiration which would trigger HttpSessionBindingListener#valueUnbound.

max-cool commented 4 years ago

What is the status on this? I'm having issues with session-scoped beans not having their @PostConstruct or @PreDestroy methods called with Spring Session, and I'm not sure if the issue is related.

D3K2MK7 commented 3 years ago

I have the same problem using SpringSession 2.5.1. When on HttpSession is invocked method invalidate() in bean below method after() is not invocked.

@Service
@SessionScope
public class SessionInfoService implements Serializable {
    @PreDestroy
    public void after() {
        System.out.println("!!!! SessionInfoService - PreDestroy");
    }
}

When I does not use SpringSession method after() is invocked.

When this will be fix?

thaarbach commented 2 months ago

Is there any update on this issue? Struggle with DestructionCallbackBindingListener, which has no Creators, like default constructor. Wrote a MixIn which works but now i got

org.springframework.data.redis.serializer.SerializationException: Could not read JSON:Cannot construct instance of `org.springframework.beans.factory.support.DisposableBeanAdapter` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Using GenericJackson2JsonRedisSerializer.

It is also not working with JdkSerializationRedisSerializer. The @PreDestroy in a SessionScoped Bean is not invoked

marcusdacoregio commented 2 months ago

I see that this problem has been reported for Spring Session 2.1.1 which is out of support at this moment. It would be great if someone could provide a minimal, reproducible sample using any of the supported versions.

thaarbach commented 2 months ago

@marcusdacoregio thanks for you quick response! We are using Spring Session 3.1.1.

Will try to create a minimal reproducible.

thaarbach commented 2 months ago

@marcusdacoregio Based on the spring-session-samples/spring-session-sample-boot-redis-json i've created a demonstrator.

Take a look https://github.com/thaarbach/spring-session-1256

I'm not sure if this is really an issue/bug of spring-session or a missunderstanding.

thaarbach commented 2 months ago

@marcusdacoregio Did you have time to take a look at this?