public void closeConnection(Connection connection) { try { connection.close(); } catch (SQLException e) { LOG.error("Can't close JDBC connection to Cassandra", e); } }
public List<Activity> listActivities(String contextId, String tenantDomain) { List<Activity> activities = null; Connection connection = getConnection(); if (connection != null) { PreparedStatement statement = null; ResultSet resultSet = null; try { statement = connection.prepareStatement(SELECT_CQL); statement.setString(1, contextId); resultSet = statement.executeQuery(); activities = new ArrayList<Activity>(); while (resultSet.next()) { JsonObject body = (JsonObject) parser.parse(resultSet.getString(BODY_COLUMN)); String tenant = getTenant(body); if (tenantDomain.equals(tenant)) { Activity activity = new Activity(body.getAsJsonObject(), resultSet.getInt(TIMESTAMP_COLUMN)); activities.add(activity); } } } catch (SQLException e) { String message = e.getMessage(); // we'll ignore the "Keyspace EVENT_KS does not exist" error, // this happens when there are 0 activities in Cassandra. if (!(message.startsWith("Keyspace ") && message.endsWith(" does not exist"))) { LOG.error("Can't retrieve activities form cassandra.", e); } } finally { try { if (statement != null) { statement.close(); } if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // ignore } closeConnection(connection); } } if (activities != null) { return activities; } else { return Collections.emptyList(); } }
public void makeIndexes(String column) { Connection connection = getConnection(); if (connection != null) { Statement statement = null; try { statement = connection.createStatement(); statement.executeUpdate( "CREATE INDEX ON " + STREAM_NAME_IN_CASSANDRA + " ('payload_" + column + "')"); } catch (SQLException e) { LOG.error("Can't create indexes.", e); } finally { try { if (statement != null) { statement.close(); } } catch (SQLException e) { // ignore } } } }