Package gov.usgs.earthquake.util
Class JDBCConnection
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.util.JDBCConnection
-
- All Implemented Interfaces:
Configurable,java.lang.AutoCloseable
- Direct Known Subclasses:
JDBCNotificationIndex,JDBCProductIndex,JsonNotificationIndex,JsonProductStorage,TrackingIndex
public class JDBCConnection extends DefaultConfigurable implements java.lang.AutoCloseable
Utility class for JDBC Connection. Sub-classes must implement the connect method, and extend startup and shutdown methods. TheverifyConnection()method tests whether the connection is active, and will shutdown() and startup() to reinitialize if it is not active.
-
-
Constructor Summary
Constructors Constructor Description JDBCConnection()Create a new JDBCConnection object.JDBCConnection(java.lang.String driver, java.lang.String url)Create a new JDBCConnection object with specific driver and URL
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTransaction()Open a transaction on the database connectionvoidclose()Implement autocloseable.voidcommitTransaction()Finalize the transaction by committing all the changes and closing the transaction.voidconfigure(Config config)Implement Configurableprotected java.sql.Connectionconnect()Connect to the database.java.sql.ConnectiongetConnection()java.lang.StringgetDriver()java.lang.StringgetUrl()voidrollbackTransaction()Undo all of the changes made during the current transactionvoidsetDriver(java.lang.String driver)voidsetUrl(java.lang.String url)voidshutdown()Shutdown the database connection.voidstartup()Initialize the database connection.java.sql.ConnectionverifyConnection()Check whether database connection is closed, and reconnect if needed.-
Methods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
-
-
-
-
Constructor Detail
-
JDBCConnection
public JDBCConnection()
Create a new JDBCConnection object.
-
JDBCConnection
public JDBCConnection(java.lang.String driver, java.lang.String url)Create a new JDBCConnection object with specific driver and URL- Parameters:
driver- String of driverurl- String of URL
-
-
Method Detail
-
close
public void close() throws java.lang.ExceptionImplement autocloseable. Callsshutdown().- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.lang.Exception- Exception
-
configure
public void configure(Config config) throws java.lang.Exception
Implement Configurable- Specified by:
configurein interfaceConfigurable- Overrides:
configurein classDefaultConfigurable- Parameters:
config- Config to set driver and URL in- Throws:
java.lang.Exception- Exception
-
connect
protected java.sql.Connection connect() throws java.lang.ExceptionConnect to the database. Sub-classes determine how connection is made.- Returns:
- the connection.
- Throws:
java.lang.Exception- if unable to connect.
-
startup
public void startup() throws java.lang.ExceptionInitialize the database connection. Sub-classes should call super.startup(), before preparing any statements.- Specified by:
startupin interfaceConfigurable- Overrides:
startupin classDefaultConfigurable- Throws:
java.lang.Exception- if error occurs
-
shutdown
public void shutdown() throws java.lang.ExceptionShutdown 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:
shutdownin interfaceConfigurable- Overrides:
shutdownin classDefaultConfigurable- Throws:
java.lang.Exception- if error occurs
-
beginTransaction
public void beginTransaction() throws java.lang.ExceptionOpen a transaction on the database connection- Throws:
java.lang.Exception- if error occurs
-
commitTransaction
public void commitTransaction() throws java.lang.ExceptionFinalize the transaction by committing all the changes and closing the transaction.- Throws:
java.lang.Exception- if error occurs
-
rollbackTransaction
public void rollbackTransaction() throws java.lang.ExceptionUndo all of the changes made during the current transaction- Throws:
java.lang.Exception- if error occurs
-
getConnection
public java.sql.Connection getConnection()
- Returns:
- current connection object, or null if not connected.
-
verifyConnection
public java.sql.Connection verifyConnection() throws java.lang.ExceptionCheck 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:
java.lang.Exception- if unable to (re)connect.
-
getDriver
public java.lang.String getDriver()
- Returns:
- driver
-
setDriver
public void setDriver(java.lang.String driver)
- Parameters:
driver- Driver to set
-
getUrl
public java.lang.String getUrl()
- Returns:
- URL
-
setUrl
public void setUrl(java.lang.String url)
- Parameters:
url- URL to set
-
-