Example usage for java.sql Connection isValid

List of usage examples for java.sql Connection isValid

Introduction

In this page you can find the example usage for java.sql Connection isValid.

Prototype

boolean isValid(int timeout) throws SQLException;

Source Link

Document

Returns true if the connection has not been closed and is still valid.

Usage

From source file:com.mirth.connect.connectors.jdbc.JdbcUtils.java

/**
 * Tests if the given connection is valid and not closed
 */// ww w .jav  a 2  s .c  o  m
public static boolean isValidConnection(Connection connection) {
    /*
     * Check if the connection is still valid. JDBC driver throws an unexpected error when
     * calling isValid for some drivers (i.e. informix), so assume the connection is not valid
     * if an exception occurs
     */
    try {
        return connection.isValid(10000);
    } catch (Throwable t) {
        return false;
    }
}

From source file:com.tera.common.database.dbcp.CPoolableConnectionFactory.java

@Override
public void validateConnection(Connection conn) throws SQLException {
    if (conn.isClosed())
        throw new SQLException("validateConnection: connection closed");
    if (validationTimeout >= 0 && !conn.isValid(validationTimeout))
        throw new SQLException("validateConnection: connection invalid");
}

From source file:org.jgrades.data.service.DataSourceServiceImpl.java

@Override
public boolean testConnection() {
    try {//from w w  w  .  jav a  2 s  .  c om
        DataSourceDetails dsDetails = getDataSourceDetails();
        Connection connection = DriverManager.getConnection(dsDetails.connectionUrl(), dsDetails.getUsername(),
                dsDetails.getPassword());
        return connection.isValid(Integer.parseInt(timeout));
    } catch (SQLException e) {
        LOGGER.error("Problem while testing connection to database", e);
        return false;
    }
}

From source file:com.aionemu.commons.database.PoolableConnectionFactoryAE.java

/**
 * Validate connection by checking if connection is not closed and is still valid. throws SQLException if connection
 * is invalid.//from  w w w  .j ava  2s.c o m
 */
@Override
public void validateConnection(Connection conn) throws SQLException {
    if (conn.isClosed())
        throw new SQLException("validateConnection: connection closed");
    if (validationTimeout >= 0 && !conn.isValid(validationTimeout))
        throw new SQLException("validateConnection: connection invalid");
}

From source file:org.apache.sqoop.mapreduce.db.BasicRetrySQLFailureHandler.java

/**
 * Verify the provided connection is valid.
 *//*from w w  w.  j  a v  a2  s . com*/
protected boolean validateConnection(Connection connection) throws SQLException {
    return connection != null && !connection.isClosed() && connection.isValid(DEFAULT_RETRY_WAIT_INTERVAL);
}

From source file:org.apache.kylin.query.adhoc.JdbcConnectionFactory.java

@Override
public boolean validateObject(Object pooledObject) {
    if (pooledObject instanceof Connection) {
        Connection connection = (Connection) pooledObject;

        if (connection != null) {
            try {
                return ((!connection.isClosed()) && (connection.isValid(1)));
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage(), e);
            }//w  w  w .ja  v  a 2 s  .c  om
        }
    }

    return false;
}

From source file:com.sky.projects.pool.jdbc.JdbcConnectionFactory.java

@Override
public boolean validateObject(PooledObject<Connection> p) {
    Connection connection = p.getObject();
    if (connection != null)
        try {//from   ww  w .  ja v a 2s .c om
            return ((!connection.isClosed()) && (connection.isValid(1)));
        } catch (SQLException e) {
            return false;
        }
    return false;
}

From source file:net.certifi.audittablegen.HsqldbDMR.java

/**
 * When KeepOpen is true, ensures that the connection is working. When KeepOpen is false, attempts a temporary
 * connection to validate the DataSource, but does not keep it open.
 *
 * @return true if a connection to the source can be established false if a connection cannot be established
 *///from  w  w  w  . ja va  2  s  .  c o  m
//@Override
public boolean ensureConnection() {

    Connection conn;

    try {
        conn = dataSource.getConnection();
        if (conn != null && conn.isValid(15)) {
            conn.close();
            return true;
        }

    } catch (SQLException ex) {
        logger.error("Error ensuring connection", ex);
    }

    return false;
}

From source file:org.darkphoenixs.pool.jdbc.JdbcConnectionFactory.java

@Override
public boolean validateObject(PooledObject<Connection> p) {

    Connection connection = p.getObject();

    if (connection != null)
        try {//from  ww w .  j  a v a  2 s.c  o  m
            return ((!connection.isClosed()) && (connection.isValid(1)));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    return false;
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testIsValid() throws Exception {
    Connection conn = new MyProxy();

    try {//from   ww w.j a  v a2 s  . c  o m
        try {
            conn.isValid(1);
        } catch (SQLException e) {

        }
    } finally {
        JdbcUtil.closeQuietly(conn);
    }
}