protected synchronized void writeToDB(PingData data, String clustername, boolean overwrite) { final String ownAddress = addressAsString(data.getAddress()); final Connection connection = getConnection(); if (connection != null) { try { if(overwrite) delete(connection, clustername, ownAddress); else { if(contains(clustername, data.getAddress())) return; } insert(connection, data, clustername, ownAddress); } catch (SQLException e) { log.error(Util.getMessage("ErrorUpdatingJDBCPINGTable"), e); } finally { closeConnection(connection); } } else { log.error(Util.getMessage("FailedToStorePingDataInDatabase")); } }
@Override public void init() throws Exception { super.init(); verifyConfigurationParameters(); // If dataSource is already set, skip loading driver or JNDI lookup if (dataSource == null) { if (stringIsEmpty(datasource_jndi_name)) { loadDriver(); } else { dataSource = getDataSourceFromJNDI(datasource_jndi_name.trim()); } } attemptSchemaInitialization(); }
protected void removeAll(String clustername) { clearTable(clustername); }
protected void remove(String clustername, Address addr) { final String addressAsString = addressAsString(addr); try { delete(clustername, addressAsString); } catch (SQLException e) { log.error("Error", e); } }
protected void readAll(List<Address> members, String clustername, Responses responses) { final Connection connection = getConnection(); if (connection != null) { try { readAll(connection, members, clustername, responses); } catch (SQLException e) { log.error(Util.getMessage("ErrorReadingJDBCPINGTable"), e); } finally { closeConnection(connection); } } }
protected void delete(String clustername, String addressToDelete) throws SQLException { final Connection connection = getConnection(); if (connection != null) { try { delete(connection, clustername, addressToDelete); } catch (SQLException e) { log.error(Util.getMessage("ErrorUpdatingJDBCPINGTable"), e); } finally { closeConnection(connection); } } else { log.error(Util.getMessage("FailedToDeletePingDataInDatabase")); } }
protected void readAll(Connection connection, List<Address> members, String clustername, Responses rsps) throws SQLException { try (PreparedStatement ps=prepareStatement(connection, select_all_pingdata_sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) { ps.setString(1, clustername); if(log.isTraceEnabled()) byte[] bytes=resultSet.getBytes(1); try { PingData data=deserialize(bytes); reads++; if(data == null || (members != null && !members.contains(data.getAddress()))) rsps.addResponse(data, false); if(local_addr != null && !local_addr.equals(data.getAddress())) addDiscoveryResponseToCaches(data.getAddress(), data.getLogicalName(), data.getPhysicalAddr());
protected boolean contains(String cluster_name, Address addr) { final String addressAsString = addressAsString(addr); try(Connection conn=getConnection()) { try (PreparedStatement ps=conn.prepareStatement(contains_sql)) { ps.setString(1, cluster_name); ps.setString(2, addressAsString); try (ResultSet resultSet=ps.executeQuery()) { if(!resultSet.next()) return false; int count=resultSet.getInt("RECORDCOUNT"); return count > 0; } } } catch(SQLException e) { log.error(Util.getMessage("ErrorReadingTable"), e); } return false; }
protected void attemptSchemaInitialization() { if(stringIsEmpty(initialize_sql)) { log.debug("Table creation step skipped: initialize_sql property is missing"); return; } Connection connection=getConnection(); if(connection == null) return; try(PreparedStatement ps=connection.prepareStatement(initialize_sql)) { if(log.isTraceEnabled()) log.trace("SQL for initializing schema: %s", ps); ps.execute(); log.debug("Table created for JDBC_PING Discovery Protocol"); } catch(SQLException e) { log.debug("Could not execute initialize_sql statement; not necessarily an error, we always attempt to create the schema. " + "To suppress this message, set initialize_sql to an empty value. Cause: %s", e.getMessage()); } finally { try { connection.close(); } catch(SQLException e) { log.error(Util.getMessage("ErrorClosingConnection"), e); } } }
PreparedStatement ps=prepareStatement(c, select, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) { ps.setString(1, cluster); try(ResultSet resultSet=ps.executeQuery()) { byte[] bytes=resultSet.getBytes(1); try { PingData data=deserialize(bytes); System.out.printf("%d %s\n", index++, data);
protected void clearTable(String clustername) { try(Connection conn=getConnection()) { try (PreparedStatement ps=conn.prepareStatement(clear_sql)) { // check presence of cluster_name parameter for backwards compatibility if (clear_sql.indexOf('?') >= 0) { ps.setString(1, clustername); } else { log.debug("Please update your clear_sql to include cluster_name parameter."); } if(log.isTraceEnabled()) log.trace("%s: SQL for clearing the table: %s", local_addr, ps); ps.execute(); } } catch(SQLException e) { log.error(Util.getMessage("ErrorClearingTable"), e); } }
protected void delete(String clustername, String addressToDelete) throws SQLException { final Connection connection = getConnection(); if (connection != null) { try { delete(connection, clustername, addressToDelete); } catch (SQLException e) { log.error(Util.getMessage("ErrorUpdatingJDBCPINGTable"), e); } finally { closeConnection(connection); } } else { log.error(Util.getMessage("FailedToDeletePingDataInDatabase")); } }
protected void readAll(List<Address> members, String clustername, Responses responses) { final Connection connection = getConnection(); if (connection != null) { try { readAll(connection, members, clustername, responses); } catch (SQLException e) { log.error(Util.getMessage("ErrorReadingJDBCPINGTable"), e); } finally { closeConnection(connection); } } }
protected void readAll(Connection connection, List<Address> members, String clustername, Responses rsps) throws SQLException { try (PreparedStatement ps=prepareStatement(connection, select_all_pingdata_sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) { ps.setString(1, clustername); if(log.isTraceEnabled()) byte[] bytes=resultSet.getBytes(1); try { PingData data=deserialize(bytes); reads++; if(data == null || (members != null && !members.contains(data.getAddress()))) rsps.addResponse(data, false); if(local_addr != null && !local_addr.equals(data.getAddress())) addDiscoveryResponseToCaches(data.getAddress(), data.getLogicalName(), data.getPhysicalAddr());
protected boolean contains(String cluster_name, Address addr) { final String addressAsString = addressAsString(addr); try(Connection conn=getConnection()) { try (PreparedStatement ps=conn.prepareStatement(contains_sql)) { ps.setString(1, cluster_name); ps.setString(2, addressAsString); try (ResultSet resultSet=ps.executeQuery()) { if(!resultSet.next()) return false; int count=resultSet.getInt("RECORDCOUNT"); return count > 0; } } } catch(SQLException e) { log.error(Util.getMessage("ErrorReadingTable"), e); } return false; }
protected void attemptSchemaInitialization() { if(stringIsEmpty(initialize_sql)) { log.debug("Table creation step skipped: initialize_sql property is missing"); return; } Connection connection=getConnection(); if(connection == null) return; try(PreparedStatement ps=connection.prepareStatement(initialize_sql)) { if(log.isTraceEnabled()) log.trace("SQL for initializing schema: %s", ps); ps.execute(); log.debug("Table created for JDBC_PING Discovery Protocol"); } catch(SQLException e) { log.debug("Could not execute initialize_sql statement; not necessarily an error, we always attempt to create the schema. " + "To suppress this message, set initialize_sql to an empty value. Cause: %s", e.getMessage()); } finally { try { connection.close(); } catch(SQLException e) { log.error(Util.getMessage("ErrorClosingConnection"), e); } } }
protected void remove(String clustername, Address addr) { final String addressAsString = addressAsString(addr); try { delete(clustername, addressAsString); } catch (SQLException e) { log.error("Error", e); } }
PreparedStatement ps=prepareStatement(c, select, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) { ps.setString(1, cluster); try(ResultSet resultSet=ps.executeQuery()) { byte[] bytes=resultSet.getBytes(1); try { PingData data=deserialize(bytes); System.out.printf("%d %s\n", index++, data);
@Override protected Connection getConnection() { Connection conn = super.getConnection(); if (neverConnected.compareAndSet(false, true)){ DatabaseMetaData meta; try { meta = conn.getMetaData(); String dbName = meta.getDatabaseProductName(); if ("PostgreSQL".equals(dbName)){ this.initialize_sql = fixDdlForPostgreSQL(this.initialize_sql); }else if ("MySQL".equals(dbName)){ this.initialize_sql = fixDdlForMySQL(this.initialize_sql); } } catch (SQLException e) { log.warn("Unable to replace SQL according to database type", e); } } return conn; }
protected synchronized void writeToDB(PingData data, String clustername, boolean overwrite) { final String ownAddress = addressAsString(data.getAddress()); final Connection connection = getConnection(); if (connection != null) { try { if(overwrite) delete(connection, clustername, ownAddress); else { if(contains(clustername, data.getAddress())) return; } insert(connection, data, clustername, ownAddress); } catch (SQLException e) { log.error(Util.getMessage("ErrorUpdatingJDBCPINGTable"), e); } finally { closeConnection(connection); } } else { log.error(Util.getMessage("FailedToStorePingDataInDatabase")); } }