* @param storageNodes the {@link StorageNode storage nodes} to persist
* @throws Exception
*/
public static void persistStorageNodesIfNecessary(HashMap<String, String> serverProperties, String password,
Set<StorageNode> storageNodes) throws Exception {
DatabaseType db = null;
Connection connection = null;
Statement queryStatement = null;
ResultSet resultSet = null;
PreparedStatement insertStorageNode = null;
PreparedStatement deleteStorageNodes = null;
try {
String dbUrl = serverProperties.get(ServerProperties.PROP_DATABASE_CONNECTION_URL);
String userName = serverProperties.get(ServerProperties.PROP_DATABASE_USERNAME);
connection = getDatabaseConnection(dbUrl, userName, password);
db = DatabaseTypeFactory.getDatabaseType(connection);
if (!(db instanceof PostgresqlDatabaseType || db instanceof OracleDatabaseType)) {
throw new IllegalArgumentException("Unknown database type, can't continue: " + db);
}
// IF there are no current storage nodes then we can persist the specified storage nodes.
// IF there are current storage nodes but none are linked to resources, we replace them with
// the currently specified addresses. This allows an install or upgrade to be run again if the
// initial address(es) were incorrect.
queryStatement = connection.createStatement();
resultSet = queryStatement
.executeQuery("SELECT count(*) FROM rhq_storage_node sn WHERE NOT sn.resource_id IS NULL");
resultSet.next();
if (resultSet.getInt(1) == 0) {
connection.setAutoCommit(false);
try {
deleteStorageNodes = connection.prepareStatement("DELETE FROM rhq_storage_node");
int numRemoved = deleteStorageNodes.executeUpdate();
if (numRemoved > 0) {
LOG.info("Removed [" + numRemoved + "] storage nodes. They will be redefined now...");
}
LOG.info("Persisting to database new storage nodes for values specified in server configuration property [rhq.storage.nodes]");
insertStorageNode = connection
.prepareStatement("INSERT INTO rhq_storage_node (id, address, cql_port, operation_mode, ctime, mtime, maintenance_pending, version) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
int id = 1001;
for (StorageNode storageNode : storageNodes) {
insertStorageNode.setInt(1, id);
insertStorageNode.setString(2, storageNode.getAddress());
insertStorageNode.setInt(3, storageNode.getCqlPort());
insertStorageNode.setString(4, StorageNode.OperationMode.INSTALLED.toString());
insertStorageNode.setLong(5, System.currentTimeMillis());
insertStorageNode.setLong(6, System.currentTimeMillis());
insertStorageNode.setBoolean(7, false);
insertStorageNode.setString(8, version);
insertStorageNode.executeUpdate();
id += 1;
}
connection.commit();
} catch (SQLException e) {
LOG.error("Failed to persist to database the storage nodes specified by server configuration "
+ "property [rhq.storage.nodes]. Transaction will be rolled back.", e);
connection.rollback();
throw e;
}
} else {
LOG.info("Storage nodes already exist in database. Server configuration property [rhq.storage.nodes] will be ignored.");
}
} finally {
if (db != null) {
db.closeResultSet(resultSet);
db.closeStatement(queryStatement);
db.closeStatement(deleteStorageNodes);
db.closeStatement(insertStorageNode);
db.closeConnection(connection);
}
}
}