Search code examples
javahttpservletsstrutshttpsession

Closeable objects associated with HttpSession


I can't find any way to destroy Closeable objects associated with HttpSession properly. It would be nice to have some event or callback invoked while session is invalidating. Wonder why I need it? I have connection associated with session, and it is not connection to DB, so I can't use pool.

It seems that my case is uncomon as I didn't found any relative questions. Is it impossible to detect session close event? If so, why it is made impossible? Maybe my case is just a result of stupid approach?


Solution

  • The servlet API provides javax.servlet.http.HttpSessionListener in order that you can get notified when a session is created or destroyed.

    Another option would be that your objects in the session implement HttpSessionBindingListener. When they are unbound from the session or when the session is destroyed they will get notified and can then close themselves.