List of usage examples for org.apache.commons.dbcp2 Utils getMessage
public static String getMessage(String key)
From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnection.java
/** * Validates the connection, using the following algorithm: * <ol>// www. j a v a 2s .c o m * <li>If {@code fastFailValidation} (constructor argument) is {@code true} and * this connection has previously thrown a fatal disconnection exception, * a {@code SQLException} is thrown. </li> * <li>If {@code sql} is null, the driver's * #{@link Connection#isValid(int) isValid(timeout)} is called. * If it returns {@code false}, {@code SQLException} is thrown; * otherwise, this method returns successfully.</li> * <li>If {@code sql} is not null, it is executed as a query and if the resulting * {@code ResultSet} contains at least one row, this method returns * successfully. If not, {@code SQLException} is thrown.</li> * </ol> * @param sql validation query * @param timeout validation timeout * @throws SQLException if validation fails or an SQLException occurs during validation */ public void validate(String sql, int timeout) throws SQLException { if (_fastFailValidation && _fatalSqlExceptionThrown) { throw new SQLException(Utils.getMessage("poolableConnection.validate.fastFail")); } if (sql == null || sql.length() == 0) { if (timeout < 0) { timeout = 0; } if (!isValid(timeout)) { throw new SQLException("isValid() returned false"); } return; } if (!sql.equals(lastValidationSql)) { lastValidationSql = sql; // Has to be the innermost delegate else the prepared statement will // be closed when the pooled connection is passivated. validationPreparedStatement = getInnermostDelegateInternal().prepareStatement(sql); } if (timeout > 0) { validationPreparedStatement.setQueryTimeout(timeout); } try (ResultSet rs = validationPreparedStatement.executeQuery()) { if (!rs.next()) { throw new SQLException("validationQuery didn't return a row"); } } catch (SQLException sqle) { throw sqle; } }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnectionFactory.java
@Override public boolean validateObject(PooledObject<PoolableConnection> p) { try {/*w w w.j a v a 2 s . co m*/ validateLifetime(p); validateConnection(p.getObject()); return true; } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(Utils.getMessage("poolableConnectionFactory.validateObject.fail"), e); } return false; } }
From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java
/** * <p>Closes and releases all idle connections that are currently stored in the connection pool * associated with this data source.</p> * * <p>Connections that are checked out to clients when this method is invoked are not affected. * When client applications subsequently invoke {@link Connection#close()} to return * these connections to the pool, the underlying JDBC connections are closed.</p> * * <p>Attempts to acquire connections using {@link #getConnection()} after this method has been * invoked result in SQLExceptions.</p> * * <p>This method is idempotent - i.e., closing an already closed BasicDataSource has no effect * and does not generate exceptions.</p> * * @throws SQLException if an error occurs closing idle connections *//*from www . java 2 s . c om*/ public synchronized void close() throws SQLException { if (registeredJmxName != null) { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { mbs.unregisterMBean(registeredJmxName); } catch (JMException e) { log.warn("Failed to unregister the JMX name: " + registeredJmxName, e); } finally { registeredJmxName = null; } } closed = true; GenericObjectPool<?> oldpool = connectionPool; connectionPool = null; dataSource = null; try { if (oldpool != null) { oldpool.close(); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new SQLException(Utils.getMessage("pool.close.fail"), e); } }