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 void
beginTransaction()
Open a transaction on the database connectionvoid
close()
Implement autocloseable.void
commitTransaction()
Finalize the transaction by committing all the changes and closing the transaction.void
configure(Config config)
Implement Configurableprotected java.sql.Connection
connect()
Connect to the database.java.sql.Connection
getConnection()
java.lang.String
getDriver()
java.lang.String
getUrl()
void
rollbackTransaction()
Undo all of the changes made during the current transactionvoid
setDriver(java.lang.String driver)
void
setUrl(java.lang.String url)
void
shutdown()
Shutdown the database connection.void
startup()
Initialize the database connection.java.sql.Connection
verifyConnection()
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.Exception
Implement autocloseable. Callsshutdown()
.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.lang.Exception
- Exception
-
configure
public void configure(Config config) throws java.lang.Exception
Implement Configurable- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classDefaultConfigurable
- Parameters:
config
- Config to set driver and URL in- Throws:
java.lang.Exception
- Exception
-
connect
protected java.sql.Connection connect() throws java.lang.Exception
Connect 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.Exception
Initialize the database connection. Sub-classes should call super.startup(), before preparing any statements.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if error occurs
-
shutdown
public void shutdown() throws java.lang.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 interfaceConfigurable
- Overrides:
shutdown
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if error occurs
-
beginTransaction
public void beginTransaction() throws java.lang.Exception
Open a transaction on the database connection- Throws:
java.lang.Exception
- if error occurs
-
commitTransaction
public void commitTransaction() throws java.lang.Exception
Finalize 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.Exception
Undo 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.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:
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
-
-