Class JDBCConnection

java.lang.Object
gov.usgs.util.DefaultConfigurable
gov.usgs.earthquake.util.JDBCConnection
All Implemented Interfaces:
Configurable, AutoCloseable
Direct Known Subclasses:
JDBCNotificationIndex, JDBCProductIndex, JsonNotificationIndex, JsonProductStorage, TrackingIndex

public class JDBCConnection extends DefaultConfigurable implements AutoCloseable
Utility class for JDBC Connection. Sub-classes must implement the connect method, and extend startup and shutdown methods. The verifyConnection() method tests whether the connection is active, and will shutdown() and startup() to reinitialize if it is not active.
  • Field Details

  • Constructor Details

    • JDBCConnection

      public JDBCConnection()
      Create a new JDBCConnection object.
    • JDBCConnection

      public JDBCConnection(String driver, String url)
      Create a new JDBCConnection object with specific driver and URL
      Parameters:
      driver - String of driver
      url - String of URL
  • Method Details

    • close

      public void close() throws Exception
      Implement autocloseable. Calls shutdown().
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - Exception
    • configure

      public void configure(Config config) throws Exception
      Implement Configurable
      Specified by:
      configure in interface Configurable
      Overrides:
      configure in class DefaultConfigurable
      Parameters:
      config - Config to set driver and URL in
      Throws:
      Exception - Exception
    • connect

      protected Connection connect() throws Exception
      Connect to the database. Sub-classes determine how connection is made.
      Returns:
      the connection.
      Throws:
      Exception - if unable to connect.
    • buildUrl

      public String buildUrl(javax.json.JsonObject secret) throws UnsupportedEncodingException
      Formats a URL encodes appended values and replaces plus signs with %20. this is required because java encoding replaces spaces with +
      Parameters:
      secret - Json object
      Returns:
      formatted url string
      Throws:
      UnsupportedEncodingException
    • startup

      public void startup() throws Exception
      Initialize the database connection. Sub-classes should call super.startup(), before preparing any statements.
      Specified by:
      startup in interface Configurable
      Overrides:
      startup in class DefaultConfigurable
      Throws:
      Exception - if error occurs
    • shutdown

      public void shutdown() throws Exception
      Shutdown the database connection. Sub-classes should close any prepared statements (catching any exceptions), and then call super.shutdown() to close the database connection.
      Specified by:
      shutdown in interface Configurable
      Overrides:
      shutdown in class DefaultConfigurable
      Throws:
      Exception - if error occurs
    • beginTransaction

      public void beginTransaction() throws Exception
      Open a transaction on the database connection
      Throws:
      Exception - if error occurs
    • commitTransaction

      public void commitTransaction() throws Exception
      Finalize the transaction by committing all the changes and closing the transaction.
      Throws:
      Exception - if error occurs
    • rollbackTransaction

      public void rollbackTransaction() throws Exception
      Undo all of the changes made during the current transaction
      Throws:
      Exception - if error occurs
    • getConnection

      public Connection getConnection()
      Returns:
      current connection object, or null if not connected.
    • verifyConnection

      public Connection verifyConnection() throws Exception
      Check whether database connection is closed, and reconnect if needed. Executes the query "select 1" using the current database connection. If this doesn't succeed, reinitializes the database connection by calling shutdown() then startup().
      Returns:
      Valid connection object.
      Throws:
      Exception - if unable to (re)connect.
    • getDriver

      public String getDriver()
      Returns:
      driver
    • setDriver

      public void setDriver(String driver)
      Parameters:
      driver - Driver to set
    • getUrl

      public String getUrl()
      Returns:
      URL
    • setUrl

      public void setUrl(String url)
      Parameters:
      url - URL to set