db-ojb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Danilo Tommasina <dtommas...@risksys.com>
Subject Re: ThreadLocal causing memory leak
Date Thu, 09 Jun 2005 15:57:08 GMT
Hi again, and sorry for spamming the mailinglist :)

Here is some code that can be applied to org.apache.ojb.broker.core.PersistenceBrokerThreadMapping
to clean up the ThreadLocal stuff to be able to cleanly
shutdown/reload a web-application without leaks in the PermGen. Using following code will
no longer needs the workaround suggested in my previous mail.

Add a new static field to the class:
    private static java.util.ArrayList loadedHMs = new java.util.ArrayList();

Modifiy the following method
    public static void setCurrentPersistenceBroker(PBKey key, PersistenceBroker broker)
         throws PBFactoryException
        HashMap map = (HashMap) currentBrokerMap.get();
        WeakHashMap set = null;
        if (map == null)
            map = new HashMap();

            loadedHMs.add( map );  // ------------------ HERE IS IT
            set = (WeakHashMap) map.get(key);

        if (set == null)
            // We emulate weak HashSet using WeakHashMap
            set = new WeakHashMap();
            map.put(key, set);
        set.put(broker, null);

Add this new method:
    /** Clean up static fields and any registered ThreadLocal contents to cleanly shutdown/reload
     * within web-applications
    public static void shutdown()
        for ( Iterator it = loadedWHM.iterator(); it.hasNext(); )
            ((HashMap) it.next()).clear();
        loadedHMs = null;
        currentBrokerMap = null;

The PersistenceBrokerThreadMapping.shutdown() method must be called in your 'web application
shutdown interceptor'.
Since there are other routines in OJB that should be called when shutting down a web-application
it would be nice to have a central method for doing this.
Further routines that should be called are:
// Release all persistence broker instances
- PersistenceBrokerFactory.releaseAllInstances();
// Release all (pooled) db connections
- ConnectionFactoryFactory.getInstance().createConnectionFactory().releaseAllResources();
- And stop any eviction Thread that is still running in background (see my first post)

Please note that there are other ThreadLocal variables that are used in OJB depending on the
config. Maybe it is also necessary to clean-up these variables (I
did not test it)
- MetadataManager.threadedRepository
- MetadataManager.currentProfileKey
- JTATxManager.txRepository

Hopefully this can be of any help.

To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org

View raw message