org.datanucleus.ExecutionContextPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-core Show documentation
Show all versions of datanucleus-core Show documentation
DataNucleus Core provides the primary components of a heterogenous Java persistence solution.
It supports persistence API's being layered on top of the core functionality.
/**********************************************************************
Copyright (c) 2012 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.datanucleus.util.NucleusLogger;
/**
* Pool of ExecutionContext objects.
* By default will pool a maximum of 20 ExecutionContext objects for reuse.
* Has an optional reaper thread that cleans out the unused pooled objects every 60 secs.
*/
public class ExecutionContextPool
{
private PersistenceNucleusContext nucCtx;
private long maxIdle = 20;
private long expirationTime;
private Map recyclableECs = new ConcurrentHashMap<>();
private CleanUpThread cleaner;
public ExecutionContextPool(PersistenceNucleusContext nucCtx)
{
this.maxIdle = nucCtx.getConfiguration().getIntProperty(PropertyNames.PROPERTY_EXECUTION_CONTEXT_MAX_IDLE);
this.nucCtx = nucCtx;
this.expirationTime = 30000; // 30 seconds
// Start cleanup thread to run every 60 secs
if (nucCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_EXECUTION_CONTEXT_REAPER_THREAD))
{
cleaner = new CleanUpThread(this, expirationTime*2);
cleaner.start();
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug("Started pool of ExecutionContext (maxPool=" + maxIdle + ", reaperThread=" + (cleaner != null) + ")");
}
}
public synchronized ExecutionContext checkOut(Object owner, Map options)
{
long now = System.currentTimeMillis();
ExecutionContext ec;
if (!recyclableECs.isEmpty())
{
Iterator> recycIter = recyclableECs.entrySet().iterator();
while (recycIter.hasNext())
{
Entry recycEntry = recycIter.next();
ec = recycEntry.getKey();
if ((now - recycEntry.getValue()) > expirationTime)
{
// object has expired
recycIter.remove();
expire(ec);
ec = null;
}
else
{
if (validate(ec))
{
recycIter.remove();
ec.initialise(owner, options);
return ec;
}
// object failed validation
recycIter.remove();
expire(ec);
ec = null;
}
}
}
// no objects available, create a new one
if (nucCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_MULTITHREADED))
{
return new ExecutionContextThreadedImpl(nucCtx, owner, options);
}
return new ExecutionContextImpl(nucCtx, owner, options);
}
public synchronized void cleanUp()
{
ExecutionContext ec;
long now = System.currentTimeMillis();
Iterator> recycIter = recyclableECs.entrySet().iterator();
while (recycIter.hasNext())
{
Entry recycEntry = recycIter.next();
ec = recycEntry.getKey();
if ((now - recycEntry.getValue()) > expirationTime)
{
recycIter.remove();
expire(ec);
ec = null;
}
}
}
public synchronized void checkIn(ExecutionContext ec)
{
if (recyclableECs.size() < maxIdle)
{
recyclableECs.put(ec, System.currentTimeMillis());
}
}
protected boolean validate(ExecutionContext ec)
{
// TODO Any situations where we don't want to reuse it? Typically if it is handed back then it is usable
return true;
}
protected void expire(ExecutionContext ec)
{
}
class CleanUpThread extends Thread
{
private ExecutionContextPool pool;
private long sleepTime;
CleanUpThread(ExecutionContextPool pool, long sleepTime)
{
this.pool = pool;
this.sleepTime = sleepTime;
}
public void run()
{
while (true)
{
try
{
sleep(sleepTime);
}
catch (InterruptedException e)
{
}
pool.cleanUp();
}
}
}
}