Remove commons pool 2.x workaround

While working on SPR-12532, an extra IdentityWrapper was added to work
around a backward compatible issue between commons pool 1.x and 2.x. This
issue (POOL-283) has actually been fixed in 2.4 and their IdentityWrapper
is using object equality so our wrapper is in the way.

Looking retrospectively, the code looks all fine without the workaround
and commons pool 2.4 or later so it has been removed.
This commit is contained in:
Stephane Nicoll
2015-06-05 16:41:07 +02:00
parent 15da5efc86
commit e8441edcb7
4 changed files with 292 additions and 82 deletions

View File

@@ -43,14 +43,7 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
* meaningful validation. All exposed Commons Pool properties use the
* corresponding Commons Pool defaults.
*
* <p>Commons Pool 2.x uses object equality while Commons Pool 1.x used identity
* equality. This clearly means that Commons Pool 2 behaves differently if several
* instances having the same identity according to their {@link Object#equals(Object)}
* method are managed in the same pool. To provide a smooth upgrade, a
* backward-compatible pool is created by default; use {@link #setUseObjectEquality(boolean)}
* if you need the standard Commons Pool 2.x behavior.
*
* <p>Compatible with Apache Commons Pool 2.2
* <p>Compatible with Apache Commons Pool 2.4
*
* @author Rod Johnson
* @author Rob Harrop
@@ -81,8 +74,6 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
private boolean blockWhenExhausted = GenericObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED;
private boolean useObjectEquality;
/**
* The Apache Commons {@code ObjectPool} used to pool target objects
*/
@@ -197,24 +188,6 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
return blockWhenExhausted;
}
/**
* Set if the pool should use object equality. Commons Pool 1.x has no specific requirement in
* that regard and allows two distinct instances being equal to be put in the same pool. However,
* this behavior has changed with commons pool 2. To preserve backward compatibility, the pool
* is configured to use reference equality ({@code false}.
*/
public void setUseObjectEquality(boolean useObjectEquality) {
this.useObjectEquality = useObjectEquality;
}
/**
* Specify if the pool should use object equality. Return {@code false} if it should use
* reference equality (as it was the case for Commons Pool 1.x)
*/
public boolean isUseObjectEquality() {
return useObjectEquality;
}
/**
* Creates and holds an ObjectPool instance.
* @see #createObjectPool()
@@ -251,8 +224,7 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
*/
@Override
public Object getTarget() throws Exception {
Object o = this.pool.borrowObject();
return (isUseObjectEquality() ? o : ((IdentityWrapper)o).target);
return this.pool.borrowObject();
}
/**
@@ -260,8 +232,7 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
*/
@Override
public void releaseTarget(Object target) throws Exception {
Object value = (isUseObjectEquality() ? target : new IdentityWrapper(target));
this.pool.returnObject(value);
this.pool.returnObject(target);
}
@Override
@@ -291,9 +262,7 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
@Override
public PooledObject<Object> makeObject() throws Exception {
Object target = newPrototypeInstance();
Object poolValue = (isUseObjectEquality() ? target : new IdentityWrapper(target));
return new DefaultPooledObject<Object>(poolValue);
return new DefaultPooledObject<Object>(newPrototypeInstance());
}
@Override
@@ -314,32 +283,4 @@ public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implem
public void passivateObject(PooledObject<Object> p) throws Exception {
}
/**
* Wraps the target type in the pool to restore the behavior of commons-pool 1.x.
*/
private static class IdentityWrapper {
private final Object target;
public IdentityWrapper(Object target) {
this.target = target;
}
@Override
public int hashCode() {
return System.identityHashCode(this.target);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdentityWrapper that = (IdentityWrapper) o;
return !(target != null ? !(target == that.target) : that.target != null);
}
}
}