
com.senzing.sql.PoolConnectionProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of senzing-commons Show documentation
Show all versions of senzing-commons Show documentation
Utility classes and functions common to multiple Senzing projects.
The newest version!
package com.senzing.sql;
import com.senzing.util.Timers;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
/**
* A {@link ConnectionProvider} implementation that is backed by a {@link
* ConnectionPool} with an optional maximum wait time.
*/
public class PoolConnectionProvider implements ConnectionProvider {
/**
* Constant used for converting nano seconds to milliseconds.
*/
private static final long ONE_MILLION = 1000000L;
/**
* The {@link ConnectionPool} to back this instance.
*/
private ConnectionPool pool;
/**
* The configured maximum wait time, or zero if no-wait and negative if
* waiting indefinitely.
*/
private long maxWait = 0L;
/**
* Constructs with the specified {@link ConnectionPool} to back this instance.
* The constructed instance will wait indefinitely for a {@link Connection}
* to become available.
*
* @param pool The {@link ConnectionPool} to use.
*
* @throws NullPointerException If the specified {@link ConnectionPool} is
* null
.
*/
public PoolConnectionProvider(ConnectionPool pool)
throws NullPointerException
{
this(pool, -1L);
}
/**
* Constructs with the specified {@link ConnectionPool} and the maximum wait
* time for waiting for a {@link Connection} from the pool. To specify
* that no wait should be done, specify zero (0) as the wait time. To
* specify that the provider should wait indefinitely then specify a negative
* number.
*
* @param pool The {@link ConnectionPool} to use.
* @param maxWait The maximum number of milliseconds to wait for a
* {@link Connection}, or zero (0) if no wait should be done,
* or a negative number to indicate an indefinite wait time.
* @throws NullPointerException If the specified {@link ConnectionPool} is
* null
.
*/
public PoolConnectionProvider(ConnectionPool pool, long maxWait) {
Objects.requireNonNull(pool, "The specified pool cannot be null");
this.pool = pool;
this.maxWait = maxWait;
}
/**
* Implemented to get the {@link Connection} within the optionally configured
* maximum wait time. If a {@link Connection} cannot be obtained then an
* exception is thrown.
*
* @return The {@link Connection} that was obtained.
* @throws SQLException If a failure occurs.
*/
public Connection getConnection() throws SQLException {
long startTime = System.nanoTime();
Connection conn = this.pool.acquire(this.getMaximumWaitTime());
long elapsedMillis = (System.nanoTime() - startTime) / ONE_MILLION;
if (conn == null) {
throw new SQLException(
"The connection could not be obtained within the allotted time. "
+ "maximumWait=[ " + this.getMaximumWaitTime() + " ], elapsed=[ "
+ elapsedMillis + " ]");
}
return conn;
}
/**
* Gets the maximum number of milliseconds to wait for a {@link Connection}
* before throwing an exception.
*
* @return The maximum number of milliseconds to wait for a {@link Connection}
* before throwing an exception.
*/
public long getMaximumWaitTime() {
return this.maxWait;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy