use of cn.taketoday.jdbc.CannotGetJdbcConnectionException in project today-framework by TAKETODAY.
the class DatabaseStartupValidator method afterPropertiesSet.
/**
* Check whether the validation query can be executed on a Connection
* from the specified DataSource, with the specified interval between
* checks, until the specified timeout.
*/
@Override
public void afterPropertiesSet() {
if (this.dataSource == null) {
throw new IllegalArgumentException("Property 'dataSource' is required");
}
try {
boolean validated = false;
long beginTime = System.currentTimeMillis();
long deadLine = beginTime + TimeUnit.SECONDS.toMillis(this.timeout);
SQLException latestEx = null;
while (!validated && System.currentTimeMillis() < deadLine) {
Connection con = null;
Statement stmt = null;
try {
con = this.dataSource.getConnection();
if (con == null) {
throw new CannotGetJdbcConnectionException("Failed to execute validation: " + "DataSource returned null from getConnection(): " + this.dataSource);
}
if (this.validationQuery == null) {
validated = con.isValid(this.interval);
} else {
stmt = con.createStatement();
stmt.execute(this.validationQuery);
validated = true;
}
} catch (SQLException ex) {
latestEx = ex;
if (logger.isDebugEnabled()) {
if (this.validationQuery != null) {
logger.debug("Validation query [{}] threw exception", validationQuery, ex);
} else {
logger.debug("Validation check threw exception", ex);
}
}
if (logger.isInfoEnabled()) {
float rest = ((float) (deadLine - System.currentTimeMillis())) / 1000;
if (rest > this.interval) {
logger.info("Database has not started up yet - retrying in {} seconds (timeout in {} seconds)", interval, rest);
}
}
} finally {
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
if (!validated) {
TimeUnit.SECONDS.sleep(this.interval);
}
}
if (!validated) {
throw new CannotGetJdbcConnectionException("Database has not started up within " + this.timeout + " seconds", latestEx);
}
if (logger.isInfoEnabled()) {
float duration = ((float) (System.currentTimeMillis() - beginTime)) / 1000;
logger.info("Database startup detected after {} seconds", duration);
}
} catch (InterruptedException ex) {
// Re-interrupt current thread, to allow other threads to react.
Thread.currentThread().interrupt();
}
}
use of cn.taketoday.jdbc.CannotGetJdbcConnectionException in project today-infrastructure by TAKETODAY.
the class DatabaseStartupValidator method afterPropertiesSet.
/**
* Check whether the validation query can be executed on a Connection
* from the specified DataSource, with the specified interval between
* checks, until the specified timeout.
*/
@Override
public void afterPropertiesSet() {
if (this.dataSource == null) {
throw new IllegalArgumentException("Property 'dataSource' is required");
}
try {
boolean validated = false;
long beginTime = System.currentTimeMillis();
long deadLine = beginTime + TimeUnit.SECONDS.toMillis(this.timeout);
SQLException latestEx = null;
while (!validated && System.currentTimeMillis() < deadLine) {
Connection con = null;
Statement stmt = null;
try {
con = this.dataSource.getConnection();
if (con == null) {
throw new CannotGetJdbcConnectionException("Failed to execute validation: " + "DataSource returned null from getConnection(): " + this.dataSource);
}
if (this.validationQuery == null) {
validated = con.isValid(this.interval);
} else {
stmt = con.createStatement();
stmt.execute(this.validationQuery);
validated = true;
}
} catch (SQLException ex) {
latestEx = ex;
if (logger.isDebugEnabled()) {
if (this.validationQuery != null) {
logger.debug("Validation query [{}] threw exception", validationQuery, ex);
} else {
logger.debug("Validation check threw exception", ex);
}
}
if (logger.isInfoEnabled()) {
float rest = ((float) (deadLine - System.currentTimeMillis())) / 1000;
if (rest > this.interval) {
logger.info("Database has not started up yet - retrying in {} seconds (timeout in {} seconds)", interval, rest);
}
}
} finally {
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
if (!validated) {
TimeUnit.SECONDS.sleep(this.interval);
}
}
if (!validated) {
throw new CannotGetJdbcConnectionException("Database has not started up within " + this.timeout + " seconds", latestEx);
}
if (logger.isInfoEnabled()) {
float duration = ((float) (System.currentTimeMillis() - beginTime)) / 1000;
logger.info("Database startup detected after {} seconds", duration);
}
} catch (InterruptedException ex) {
// Re-interrupt current thread, to allow other threads to react.
Thread.currentThread().interrupt();
}
}
Aggregations