Class DatabaseConfiguration.JdbcOperation<T>
- java.lang.Object
-
- de.aristaflow.adept2.base.registry.dbconfiguration.DatabaseConfiguration.JdbcOperation<T>
-
- Type Parameters:
T
- the type of the results produced by a JDBC operation
- Enclosing class:
- DatabaseConfiguration
protected abstract class DatabaseConfiguration.JdbcOperation<T> extends Object
An internally used helper class for simplifying database access through plain JDBC. This class provides a simple framework for creating and executing a JDBC statement. It especially takes care of proper handling of JDBC resources even in case of an error.
-
-
Field Summary
Fields Modifier and Type Field Description protected ExtendedConnection
conn
Stores the connection.protected org.apache.commons.configuration2.event.EventType<? extends org.apache.commons.configuration2.event.ConfigurationErrorEvent>
errorEventType
The type of the event to send in case of an error.protected String
errorPropertyName
The property configurationName for an error event.protected Object
errorPropertyValue
The property value for an error event.protected org.apache.commons.configuration2.event.EventType<?>
operationEventType
The type of the operation which caused an error.protected PreparedStatement
pstmt
Stores the statement.protected boolean
readOnly
Whether a read-only connection suffices for the JDBC operation.protected ResultSet
resultSet
Stores the result set.
-
Constructor Summary
Constructors Modifier Constructor Description protected
JdbcOperation(boolean readOnly, org.apache.commons.configuration2.event.EventType<? extends org.apache.commons.configuration2.event.ConfigurationErrorEvent> errEvType, org.apache.commons.configuration2.event.EventType<?> opType, String errPropName, Object errPropVal)
Creates a new instance ofJdbcOperation
and initializes the properties related to the error event.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected PreparedStatement
createStatement(String sql, boolean nameCol)
Creates aPreparedStatement
object for executing the specified SQL statement.T
execute()
Executes this operation.protected Connection
getConnection()
Returns the current connection.protected PreparedStatement
initStatement(String sql, boolean nameCol, Object... params)
Creates an initializes aPreparedStatement
object for executing an SQL statement.protected ResultSet
openResultSet(String sql, boolean nameCol, Object... params)
Creates aPreparedStatement
for a query, initializes it and executes it.protected abstract T
performOperation()
Performs the JDBC operation.
-
-
-
Field Detail
-
conn
protected ExtendedConnection conn
Stores the connection.
-
pstmt
protected PreparedStatement pstmt
Stores the statement.
-
resultSet
protected ResultSet resultSet
Stores the result set.
-
errorEventType
protected final org.apache.commons.configuration2.event.EventType<? extends org.apache.commons.configuration2.event.ConfigurationErrorEvent> errorEventType
The type of the event to send in case of an error.
-
operationEventType
protected final org.apache.commons.configuration2.event.EventType<?> operationEventType
The type of the operation which caused an error.
-
errorPropertyName
protected final String errorPropertyName
The property configurationName for an error event.
-
errorPropertyValue
protected final Object errorPropertyValue
The property value for an error event.
-
readOnly
protected final boolean readOnly
Whether a read-only connection suffices for the JDBC operation.
-
-
Constructor Detail
-
JdbcOperation
protected JdbcOperation(boolean readOnly, org.apache.commons.configuration2.event.EventType<? extends org.apache.commons.configuration2.event.ConfigurationErrorEvent> errEvType, org.apache.commons.configuration2.event.EventType<?> opType, String errPropName, Object errPropVal)
Creates a new instance ofJdbcOperation
and initializes the properties related to the error event.- Parameters:
readOnly
- Whether a read-only connection suffices for the JDBC operation.errEvType
- the type of the error eventopType
- the operation event typeerrPropName
- the property configurationName for the error eventerrPropVal
- the property value for the error event
-
-
Method Detail
-
execute
public T execute()
Executes this operation. This method obtains a database connection and then delegates toperformOperation()
. Afterwards it performs the necessary clean up. Exceptions that are thrown during the JDBC operation are caught and transformed into configuration error events.- Returns:
- the result of the operation
-
getConnection
protected Connection getConnection()
Returns the current connection. This method can be called whileexecute()
is running. It returns null otherwise.- Returns:
- the current connection
-
createStatement
protected PreparedStatement createStatement(String sql, boolean nameCol) throws SQLException
Creates aPreparedStatement
object for executing the specified SQL statement.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into account- Returns:
- the prepared statement object
- Throws:
SQLException
- if an SQL error occurs
-
initStatement
protected PreparedStatement initStatement(String sql, boolean nameCol, Object... params) throws SQLException
Creates an initializes aPreparedStatement
object for executing an SQL statement. This method first callscreateStatement()
for creating the statement and then initializes the statement's parameters.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into accountparams
- the parameters for the statement- Returns:
- the initialized statement object
- Throws:
SQLException
- if an SQL error occurs
-
openResultSet
protected ResultSet openResultSet(String sql, boolean nameCol, Object... params) throws SQLException
Creates aPreparedStatement
for a query, initializes it and executes it. The resultingResultSet
is returned.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into accountparams
- the parameters for the statement- Returns:
- the
ResultSet
produced by the query - Throws:
SQLException
- if an SQL error occurs
-
performOperation
protected abstract T performOperation() throws SQLException
Performs the JDBC operation. This method is called byexecute()
after this object has been fully initialized. Here the actual JDBC logic has to be placed.- Returns:
- the result of the operation
- Throws:
SQLException
- if an SQL error occurs
-
-