Package gov.usgs.earthquake.util
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
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new JDBCConnection object.JDBCConnection
(String driver, String url) Create a new JDBCConnection object with specific driver and URL -
Method Summary
Modifier and TypeMethodDescriptionvoid
Open a transaction on the database connectionbuildUrl
(javax.json.JsonObject secret) Formats a URL encodes appended values and replaces plus signs with %20.void
close()
Implement autocloseable.void
Finalize the transaction by committing all the changes and closing the transaction.void
Implement Configurableprotected Connection
connect()
Connect to the database.getUrl()
void
Undo all of the changes made during the current transactionvoid
void
void
shutdown()
Shutdown the database connection.void
startup()
Initialize the database connection.Check whether database connection is closed, and reconnect if needed.Methods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
-
Field Details
-
DATABASE_SECRET_ARN_PROPERTY
- See Also:
-
-
Constructor Details
-
JDBCConnection
public JDBCConnection()Create a new JDBCConnection object. -
JDBCConnection
Create a new JDBCConnection object with specific driver and URL- Parameters:
driver
- String of driverurl
- String of URL
-
-
Method Details
-
close
Implement autocloseable. Callsshutdown()
.- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
- Exception
-
configure
Implement Configurable- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classDefaultConfigurable
- Parameters:
config
- Config to set driver and URL in- Throws:
Exception
- Exception
-
connect
Connect to the database. Sub-classes determine how connection is made.- Returns:
- the connection.
- Throws:
Exception
- if unable to connect.
-
buildUrl
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
Initialize the database connection. Sub-classes should call super.startup(), before preparing any statements.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classDefaultConfigurable
- Throws:
Exception
- if error occurs
-
shutdown
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:
Exception
- if error occurs
-
beginTransaction
Open a transaction on the database connection- Throws:
Exception
- if error occurs
-
commitTransaction
Finalize the transaction by committing all the changes and closing the transaction.- Throws:
Exception
- if error occurs
-
rollbackTransaction
Undo all of the changes made during the current transaction- Throws:
Exception
- if error occurs
-
getConnection
- Returns:
- current connection object, or null if not connected.
-
verifyConnection
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
- Returns:
- driver
-
setDriver
- Parameters:
driver
- Driver to set
-
getUrl
- Returns:
- URL
-
setUrl
- Parameters:
url
- URL to set
-