}
subject = (subject == null) ? defaultSubject : subject;
cri = (cri == null) ? defaultCri : cri;
ConnectionListener cl = null;
boolean verifyConnectionListener = true;
long startWait = System.currentTimeMillis();
if (cls.size() > 0)
{
if (shutdown.get())
throw new RetryableUnavailableException(
bundle.thePoolHasBeenShutdown(pool.getName(),
Integer.toHexString(System.identityHashCode(this))));
cl = cls.peek();
if (cl != null)
{
try
{
cl = cls.poll(poolConfiguration.getBlockingTimeout(), TimeUnit.MILLISECONDS);
statistics.deltaTotalBlockingTime(System.currentTimeMillis() - startWait);
}
catch (InterruptedException ie)
{
Thread.interrupted();
long end = System.currentTimeMillis() - startWait;
throw new ResourceException(bundle.interruptedWhileRequestingConnection(end));
}
}
else
{
try
{
// No, the pool was empty, so we have to make a new one.
cl = createConnectionEventListener(subject, cri);
if (trace)
log.trace("supplying new ManagedConnection: " + cl);
verifyConnectionListener = false;
}
catch (Throwable t)
{
log.throwableWhileAttemptingGetNewGonnection(cl, t);
throw new ResourceException(bundle.unexpectedThrowableWhileTryingCreateConnection(cl), t);
}
}
}
else
{
try
{
cl = cls.poll(poolConfiguration.getBlockingTimeout(), TimeUnit.MILLISECONDS);
statistics.deltaTotalBlockingTime(System.currentTimeMillis() - startWait);
if (shutdown.get())
throw new RetryableUnavailableException(
bundle.thePoolHasBeenShutdown(pool.getName(),
Integer.toHexString(System.identityHashCode(this))));
}
catch (InterruptedException ie)
{
Thread.interrupted();
if (!poolConfiguration.isUseFastFail())
{
throw new ResourceException(bundle.noMManagedConnectionsAvailableWithinConfiguredBlockingTimeout(
poolConfiguration.getBlockingTimeout()));
}
else
{
if (trace)
log.trace("Fast failing for connection attempt. No more attempts will be made to " +
"acquire connection from pool and a new connection will be created immeadiately");
try
{
cl = createConnectionEventListener(subject, cri);
if ((poolConfiguration.isPrefill() || poolConfiguration.isStrictMin()) &&
pool instanceof PrefillPool &&
poolConfiguration.getMinSize() > 0)
PoolFiller.fillPool(this);
if (trace)
log.trace("supplying new ManagedConnection: " + cl);
verifyConnectionListener = false;
}
catch (Throwable t)
{
log.throwableWhileAttemptingGetNewGonnection(cl, t);
throw new ResourceException(bundle.unexpectedThrowableWhileTryingCreateConnection(cl), t);
}
}
}
}
// Register the connection listener
checkedOut.add(cl);
// Update in used statistics
statistics.setInUsedCount(checkedOut.size());
if (!verifyConnectionListener)
{
// Return connection listener
return cl;
}
else
{
try
{
Object matchedMC =
mcf.matchManagedConnections(Collections.singleton(cl.getManagedConnection()), subject, cri);
if (matchedMC != null)
{
if (trace)
log.trace("supplying ManagedConnection from pool: " + cl);