List of usage examples for org.springframework.jdbc CannotGetJdbcConnectionException CannotGetJdbcConnectionException
public CannotGetJdbcConnectionException(String msg)
From source file:org.springframework.jdbc.support.DatabaseStartupValidator.java
/** * 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.//from ww w. ja va 2s.c om */ @Override public void afterPropertiesSet() { DataSource dataSource = this.dataSource; if (dataSource == null) { throw new IllegalArgumentException("Property 'dataSource' is required"); } if (this.validationQuery == null) { throw new IllegalArgumentException("Property 'validationQuery' is required"); } try { boolean validated = false; long beginTime = System.currentTimeMillis(); long deadLine = beginTime + this.timeout * 1000; SQLException latestEx = null; while (!validated && System.currentTimeMillis() < deadLine) { Connection con = null; Statement stmt = null; try { con = dataSource.getConnection(); if (con == null) { throw new CannotGetJdbcConnectionException("Failed to execute validation query: " + "DataSource returned null from getConnection(): " + dataSource); } stmt = con.createStatement(); stmt.execute(this.validationQuery); validated = true; } catch (SQLException ex) { latestEx = ex; logger.debug("Validation query [" + this.validationQuery + "] threw exception", ex); float rest = ((float) (deadLine - System.currentTimeMillis())) / 1000; if (rest > this.interval) { logger.warn("Database has not started up yet - retrying in " + this.interval + " seconds (timeout in " + rest + " seconds)"); } } finally { JdbcUtils.closeStatement(stmt); JdbcUtils.closeConnection(con); } if (!validated) { Thread.sleep(this.interval * 1000); } } if (!validated) { throw new CannotGetJdbcConnectionException( "Database has not started up within " + this.timeout + " seconds", latestEx); } float duration = (System.currentTimeMillis() - beginTime) / 1000; if (logger.isInfoEnabled()) { logger.info("Database startup detected after " + duration + " seconds"); } } catch (InterruptedException ex) { // Re-interrupt current thread, to allow other threads to react. Thread.currentThread().interrupt(); } }