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

Session scoped beans PreDestroy not called

Open lRawd opened this issue 6 years ago • 8 comments

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.

lRawd avatar Nov 09 '18 21:11 lRawd

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.

vpavic avatar Nov 12 '18 21:11 vpavic

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.

max-cool avatar Dec 16 '19 21:12 max-cool

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?

D3K2MK7 avatar Sep 24 '21 12:09 D3K2MK7

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

thaarbach avatar Aug 05 '24 16:08 thaarbach

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.

marcusdacoregio avatar Aug 05 '24 16:08 marcusdacoregio

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

Will try to create a minimal reproducible.

thaarbach avatar Aug 06 '24 06:08 thaarbach

@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 avatar Aug 08 '24 14:08 thaarbach

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

thaarbach avatar Sep 02 '24 07:09 thaarbach