| Constructor: |
public StackObjectPool() {
this((PoolableObjectFactory)null,DEFAULT_MAX_SLEEPING,DEFAULT_INIT_SLEEPING_CAPACITY);
}
|
public StackObjectPool(int maxIdle) {
this((PoolableObjectFactory)null,maxIdle,DEFAULT_INIT_SLEEPING_CAPACITY);
}
|
public StackObjectPool(PoolableObjectFactory factory) {
this(factory,DEFAULT_MAX_SLEEPING,DEFAULT_INIT_SLEEPING_CAPACITY);
}
Create a new StackObjectPool using
the specified factory to create new instances. |
public StackObjectPool(int maxIdle,
int initIdleCapacity) {
this((PoolableObjectFactory)null,maxIdle,initIdleCapacity);
}
Parameters:
maxIdle - cap on the number of "sleeping" instances in the pool
initIdleCapacity - initial size of the pool (this specifies the size of the container,
it does not cause the pool to be pre-populated.)
Also see:
- StackObjectPool(PoolableObjectFactory, int, int)
|
public StackObjectPool(PoolableObjectFactory factory,
int maxIdle) {
this(factory,maxIdle,DEFAULT_INIT_SLEEPING_CAPACITY);
}
Create a new SimpleObjectPool using
the specified factory to create new instances,
capping the number of "sleeping" instances to max. Parameters:
factory - the PoolableObjectFactory used to populate the pool
maxIdle - cap on the number of "sleeping" instances in the pool
|
public StackObjectPool(PoolableObjectFactory factory,
int maxIdle,
int initIdleCapacity) {
_factory = factory;
_maxSleeping = (maxIdle < 0 ? DEFAULT_MAX_SLEEPING : maxIdle);
int initcapacity = (initIdleCapacity < 1 ? DEFAULT_INIT_SLEEPING_CAPACITY : initIdleCapacity);
_pool = new Stack();
_pool.ensureCapacity( initcapacity > _maxSleeping ? _maxSleeping : initcapacity);
}
Create a new SimpleObjectPool using
the specified factory to create new instances,
capping the number of "sleeping" instances to max,
and initially allocating a container capable of containing
at least init instances. Parameters:
factory - the PoolableObjectFactory used to populate the pool
maxIdle - cap on the number of "sleeping" instances in the pool
initIdleCapacity - initial size of the pool (this specifies the size of the container,
it does not cause the pool to be pre-populated.)
|
| Method from org.apache.commons.pool.impl.StackObjectPool Detail: |
public synchronized void addObject() throws Exception {
assertOpen();
if (_factory == null) {
throw new IllegalStateException("Cannot add objects without a factory.");
}
Object obj = _factory.makeObject();
boolean success = true;
if(!_factory.validateObject(obj)) {
success = false;
} else {
_factory.passivateObject(obj);
}
boolean shouldDestroy = !success;
if (success) {
Object toBeDestroyed = null;
if(_pool.size() >= _maxSleeping) {
shouldDestroy = true;
toBeDestroyed = _pool.remove(0); // remove the stalest object
}
_pool.push(obj);
obj = toBeDestroyed; // swap returned obj with the stalest one so it can be destroyed
}
notifyAll(); // _numIdle has changed
if(shouldDestroy) { // by constructor, shouldDestroy is false when _factory is null
try {
_factory.destroyObject(obj);
} catch(Exception e) {
// ignored
}
}
}
Create an object, and place it into the pool.
addObject() is useful for "pre-loading" a pool with idle objects. |
public synchronized Object borrowObject() throws Exception {
assertOpen();
Object obj = null;
boolean newlyCreated = false;
while (null == obj) {
if (!_pool.empty()) {
obj = _pool.pop();
} else {
if(null == _factory) {
throw new NoSuchElementException();
} else {
obj = _factory.makeObject();
newlyCreated = true;
if (obj == null) {
throw new NoSuchElementException("PoolableObjectFactory.makeObject() returned null.");
}
}
}
if (null != _factory && null != obj) {
try {
_factory.activateObject(obj);
if (!_factory.validateObject(obj)) {
throw new Exception("ValidateObject failed");
}
} catch (Throwable t) {
try {
_factory.destroyObject(obj);
} catch (Throwable t2) {
// swallowed
} finally {
obj = null;
}
if (newlyCreated) {
throw new NoSuchElementException(
"Could not create a validated object, cause: " +
t.getMessage());
}
}
}
}
_numActive++;
return obj;
}
|
public synchronized void clear() {
if(null != _factory) {
Iterator it = _pool.iterator();
while(it.hasNext()) {
try {
_factory.destroyObject(it.next());
} catch(Exception e) {
// ignore error, keep destroying the rest
}
}
}
_pool.clear();
}
Clears any objects sitting idle in the pool. |
public void close() throws Exception {
super.close();
clear();
}
|
public synchronized int getNumActive() {
return _numActive;
}
Return the number of instances currently borrowed from this pool. |
public synchronized int getNumIdle() {
return _pool.size();
}
Return the number of instances
currently idle in this pool. |
public synchronized void invalidateObject(Object obj) throws Exception {
_numActive--;
if (null != _factory) {
_factory.destroyObject(obj);
}
notifyAll(); // _numActive has changed
}
|
public synchronized void returnObject(Object obj) throws Exception {
boolean success = !isClosed();
if(null != _factory) {
if(!_factory.validateObject(obj)) {
success = false;
} else {
try {
_factory.passivateObject(obj);
} catch(Exception e) {
success = false;
}
}
}
boolean shouldDestroy = !success;
_numActive--;
if (success) {
Object toBeDestroyed = null;
if(_pool.size() >= _maxSleeping) {
shouldDestroy = true;
toBeDestroyed = _pool.remove(0); // remove the stalest object
}
_pool.push(obj);
obj = toBeDestroyed; // swap returned obj with the stalest one so it can be destroyed
}
notifyAll(); // _numActive has changed
if(shouldDestroy) { // by constructor, shouldDestroy is false when _factory is null
try {
_factory.destroyObject(obj);
} catch(Exception e) {
// ignored
}
}
}
|
public synchronized void setFactory(PoolableObjectFactory factory) throws IllegalStateException {
assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
clear();
_factory = factory;
}
}
Sets the factory this pool uses
to create new instances. Trying to change
the factory while there are borrowed objects will
throw an IllegalStateException . |