Commit bad65427 authored by iwakeh's avatar iwakeh 🌴 Committed by Karsten Loesing
Browse files

Make Database AutoCloseable and use try-with-resource.

parent e9f3226f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import java.util.TimeZone;

/** Database wrapper to connect to the database, insert data, run the stored
 * procedure for aggregating data, and query aggregated data as output. */
class Database {
class Database implements AutoCloseable {

  /** Database connection string. */
  private String jdbcString;
@@ -210,8 +210,8 @@ class Database {
    return statistics;
  }

  /** Disconnect from the database. */
  void disconnect() throws SQLException {
  /** Release database connection. */
  public void close() throws SQLException {
    this.connection.close();
  }
}
+51 −58
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ public class Main {
    File historyFile = new File(Configuration.history);
    reader.setHistoryFile(historyFile);
    Parser parser = new Parser();
    Database database = new Database(Configuration.database);
    try (Database database = new Database(Configuration.database)) {
      try {
        for (Descriptor descriptor : reader.readDescriptors(
            new File(Configuration.descriptors
@@ -78,29 +78,22 @@ public class Main {
        log.error("Cannot recover from SQL exception while inserting or "
            + "aggregating data. Rolling back and exiting.", sqle);
        database.rollback();
      database.disconnect();
        return;
      }
      reader.saveHistoryFile(historyFile);

      log.info("Querying aggregated statistics from the database.");
    Iterable<OutputLine> output;
    try {
      output = database.queryServersIpv6();
    } catch (SQLException sqle) {
      log.error("Cannot recover from SQL exception while querying. Not writing "
          + "output file.", sqle);
      return;
    } finally {
      database.disconnect();
    }

      Iterable<OutputLine> output = database.queryServersIpv6();
      log.info("Writing aggregated statistics to {}.", Configuration.output);
      if (null != output) {
        new Writer().write(Paths.get(Configuration.output), output);
      }

      log.info("Terminating ipv6servers module.");
    } catch (SQLException sqle) {
      log.error("Cannot recover from SQL exception while querying. Not writing "
          + "output file.", sqle);
    }
  }
}