Managing resource closure in a servlet container

I use Tomcat as a servlet container and deploy many WARs. Many of the WARs have common base classes that are replicated in each context due to different class loaders, etc.

How can I ensure that resources are cleared when context is destroyed without hooking every web.xml file to add context listeners?

Ideally, I would like something like strings

class MyResourceHolder implements SomeListenerInterface {
    private SomeResource resource;
    {
        SomeContextThingie.registerDestructionListener(this);
    }
    public void onDestroy() { resource.close(); }
}

I could put something in every web.xml, but since there are potentially many WARs and only those that actually initialize the resource, it is necessary to clear it, it seems more natural to register for cleaning when the resource is initialized, rather than duplicating a lot of XML configuration. and then there may be a cleaning.

( SQL. , ...)

, - , Google-fu . !

+3
3

, ServletContextListener.

Servlet 3.0, @WebListener, webapp . Servlet 2.5 web.xml.

+2

shared-classloader JNDI - , , .

, , API.

. - " XClass XClass" .

Edit1: : - (, webapp ) - JDK . , , , , JVM, , , gc'd.

+1

SQL - - JNDI.

0

Source: https://habr.com/ru/post/1750035/


All Articles