Java Code Examples for java.sql.Connection.isValid()

The following are Jave code examples for showing how to use isValid() of the java.sql.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: MakeDefaultCatalogAction.java   View Source Code Vote up 6 votes
/**
 * If DDL exception was caused by a closed connection, log info and display
 * a simple error dialog. Otherwise let users report the exception.
 */
private void handleDLLException(DatabaseConnection dbConn,
        DDLException e) throws SQLException, MissingResourceException {
    Connection conn = dbConn == null ? null : dbConn.getJDBCConnection();
    if (conn != null && !conn.isValid(1000)) {
        LOGGER.log(Level.INFO, e.getMessage(), e);
        NotifyDescriptor nd = new NotifyDescriptor.Message(
                NbBundle.getMessage(
                MakeDefaultCatalogAction.class,
                "ERR_ConnectionToServerClosed"), //NOI18N
                NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notifyLater(nd);
    } else {
        Exceptions.printStackTrace(e);
    }
}
 
Example 2
Project: RollenspielAlexaSkill   File: SimpleDBConnection.java   View Source Code Vote up 6 votes
public synchronized Connection getConnection() {
	Connection result = cachedConnection; 
	if (result != null) {
		try {
			if (!result.isValid(2)) {
				logger.log(Level.WARNING, "closing invalid connection"); 
				result = null;
			}
		}
		catch (Exception e) {
			logger.log(Level.WARNING, "connection became invalid: "+e.toString(), e); 
			result = null;
		}
	}
	if (result == null) {
		result = getNewConnection();
		cachedConnection = result;
	}
	return result;
}
 
Example 3
Project: the-vigilantes   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for Bug#56122 - JDBC4 functionality failure when using replication connections.
 */
public void testBug56122() throws Exception {
    for (final Connection testConn : new Connection[] { this.conn, getFailoverConnection(), getLoadBalancedConnection(),
            getMasterSlaveReplicationConnection() }) {
        testConn.createClob();
        testConn.createBlob();
        testConn.createNClob();
        testConn.createSQLXML();
        testConn.isValid(12345);
        testConn.setClientInfo(new Properties());
        testConn.setClientInfo("NAME", "VALUE");
        testConn.getClientInfo();
        testConn.getClientInfo("CLIENT");
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createArrayOf("A_TYPE", null);
                return null;
            }
        });
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createStruct("A_TYPE", null);
                return null;
            }
        });
    }
}
 
Example 4
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for Bug#56122 - JDBC4 functionality failure when using replication connections.
 */
public void testBug56122() throws Exception {
    for (final Connection testConn : new Connection[] { this.conn, getFailoverConnection(), getLoadBalancedConnection(),
            getMasterSlaveReplicationConnection() }) {
        testConn.createClob();
        testConn.createBlob();
        testConn.createNClob();
        testConn.createSQLXML();
        testConn.isValid(12345);
        testConn.setClientInfo(new Properties());
        testConn.setClientInfo("NAME", "VALUE");
        testConn.getClientInfo();
        testConn.getClientInfo("CLIENT");
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createArrayOf("A_TYPE", null);
                return null;
            }
        });
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createStruct("A_TYPE", null);
                return null;
            }
        });
    }
}
 
Example 5
Project: OpenVertretung   File: ConnectionRegressionTest.java   View Source Code Vote up 5 votes
/**
 * Tests fix for Bug#56122 - JDBC4 functionality failure when using replication connections.
 */
public void testBug56122() throws Exception {
    for (final Connection testConn : new Connection[] { this.conn, getFailoverConnection(), getLoadBalancedConnection(),
            getMasterSlaveReplicationConnection() }) {
        testConn.createClob();
        testConn.createBlob();
        testConn.createNClob();
        testConn.createSQLXML();
        testConn.isValid(12345);
        testConn.setClientInfo(new Properties());
        testConn.setClientInfo("NAME", "VALUE");
        testConn.getClientInfo();
        testConn.getClientInfo("CLIENT");
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createArrayOf("A_TYPE", null);
                return null;
            }
        });
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createStruct("A_TYPE", null);
                return null;
            }
        });
    }
}
 
Example 6
Project: slardar   File: ConnectionUtils.java   View Source Code Vote up 5 votes
public static boolean isClose(Connection connection){
    try {
        connection.isValid(1000);
        return connection == null||connection.isClosed();
    } catch (SQLException e) {
        return false;
    }
}
 
Example 7
Project: slardar   File: ConnectionUtils.java   View Source Code Vote up 5 votes
public static boolean isValid(Connection connection){
    try {
        return connection == null||connection.isValid(3);
    } catch (SQLException e) {
        return false;
    }
}
 
Example 8
Project: AnyMall   File: DBConnectionPool.java   View Source Code Vote up 5 votes
private boolean validateConnection(Connection conn) {
	try {
		return conn.isValid(2000);
	} catch (Exception e) {
		log.error(String.format("��֤���ݿ�����״̬ʱ��������%s", e.getMessage()));
		e.printStackTrace();
		return false;
	}
}
 
Example 9
Project: BibliotecaPS   File: ConnectionRegressionTest.java   View Source Code Vote up 5 votes
/**
 * Tests fix for Bug#56122 - JDBC4 functionality failure when using replication connections.
 */
public void testBug56122() throws Exception {
    for (final Connection testConn : new Connection[] { this.conn, getFailoverConnection(), getLoadBalancedConnection(),
            getMasterSlaveReplicationConnection() }) {
        testConn.createClob();
        testConn.createBlob();
        testConn.createNClob();
        testConn.createSQLXML();
        testConn.isValid(12345);
        testConn.setClientInfo(new Properties());
        testConn.setClientInfo("NAME", "VALUE");
        testConn.getClientInfo();
        testConn.getClientInfo("CLIENT");
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createArrayOf("A_TYPE", null);
                return null;
            }
        });
        assertThrows(SQLFeatureNotSupportedException.class, new Callable<Void>() {
            public Void call() throws Exception {
                testConn.createStruct("A_TYPE", null);
                return null;
            }
        });
    }
}
 
Example 10
Project: aliyun-maxcompute-data-collectors   File: BasicRetrySQLFailureHandler.java   View Source Code Vote up 4 votes
/**
 * Verify the provided connection is valid.
 */
protected boolean validateConnection(Connection connection)
    throws SQLException {
  return connection != null && !connection.isClosed()
    && connection.isValid(DEFAULT_RETRY_WAIT_INTERVAL);
}