Package com.saperion.common.sql
Class DataSource<CommonAccessException extends Exception,CommonReadResultException extends Exception>
java.lang.Object
com.saperion.common.sql.DataSource<CommonAccessException,CommonReadResultException>
- Type Parameters:
CommonAccessException- common type of all providedExceptionsduring connection-handling and sql-executionCommonReadResultException- common type of all usedResultReaders duringexecuteQuery(String, ResultReader)-operations to signal business-exceptions duringResultReader.readResult(ResultSet)
- Direct Known Subclasses:
SimpleDataSource
public abstract class DataSource<CommonAccessException extends Exception,CommonReadResultException extends Exception>
extends Object
DataSource encapsulates a DataSource and provides simple SQL-execution-methods (
executeUpdate(String), executeQuery(String, ResultReader)) with all the necessary error treatment.
This class contains a lot of methods signaling events in its work flow or requesting for concrete Exceptions
in case of an error. This way subclasses may fine-tune the logging, collecting of runtime-informations and
error-reporting.
A simple implementation that can be used directly and that has no logging, no runtime-information and treats all
errors as IllegalStateExceptions is SimpleDataSource. While this class is easy to use, it is still
strongly recommended to write your own subclass in any real project.
- Author:
- agz
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDataSource(DataSource dataSource) Creates a newDataSourceencapsulating the specifiedDataSource. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidafterExecuteQuery(String sql) This event-method is called immediately afterStatement.executeQuery(String)was successfully called.protected abstract voidafterExecuteUpdate(String sql, int rowCount) This event-method is called immediately afterStatement.executeUpdate(String)was successfully called.protected abstract voidafterReadResult(String sql, int rowCount) This event-method is called immediately afterResultReader.readResult(ResultSet)was successfully called.It is called afternoteReadResultTime(String, long, boolean).protected abstract voidbeforeExecuteQuery(String sql) This event-method is called immediately beforeStatement.executeQuery(String)is called.protected abstract voidThis event-method is called immediately beforeStatement.executeUpdate(String)is called.protected abstract voidbeforeReadResult(String sql) This event-method is called immediately beforeResultReader.readResult(ResultSet)is called.voidexecuteQuery(String sql, ResultReader<? extends CommonReadResultException> resultReader) Executes the specified SQL-select statement and calls the specifiedResultReader.intexecuteUpdate(String sql) Executes the specified SQL-update statement and returns the count of manipulated rows.Returns the underlying rawDataSource.Returns theSqlDialectof the underlyingDataSource.protected abstract voidnoteExecuteQueryTime(String sql, long timeNanoseconds, boolean success) This event-method is called immediately afterStatement.executeQuery(String)is called, no matter whether the call was successful or not.protected abstract voidnoteExecuteUpdateTime(String sql, long timeNanoseconds, boolean success) This event-method is called immediately afterStatement.executeUpdate(String)is called, no matter whether the call was successful or not.protected abstract voidnoteReadResultTime(String sql, long timeNanoseconds, boolean success) This event-method is called immediately afterResultReader.readResult(ResultSet)is called, no matter whether the call was successful or not.protected abstract CommonAccessExceptiononErrorCreatingStatement(Throwable t, String sql) This event-method is called by all SQL-execution-methods if there is noStatementavailable from the underlyingConnection.protected abstract CommonAccessExceptiononErrorExecutingQuery(Throwable t, String sql) This event-method is called byexecuteQuery(String, ResultReader)if there is any error executing the statement.protected abstract CommonAccessExceptiononErrorExecutingUpdate(Throwable t, String sql) This event-method is called byexecuteUpdate(String)if there is any error executing the statement.protected abstract CommonAccessExceptiononErrorGettingConnection(Throwable t, String sql) This event-method is called by all SQL-execution-methods if there is noConnectionavailable from the underlyingDataSource.protected abstract CommonAccessExceptionThis event-method is called bygetSqlDialect()if there is any error getting the database product name.protected abstract CommonAccessExceptionThis event-method is called bygetSqlDialect()if there is any error getting theDatabaseMetaData.protected abstract CommonAccessExceptiononErrorReadingResultSet(Throwable t, String sql) This event-method is called byexecuteQuery(String, ResultReader)if there is any error reading theResultSet.
-
Field Details
-
LOGGER
-
-
Constructor Details
-
DataSource
Creates a newDataSourceencapsulating the specifiedDataSource. The specifiedDataSourcemust not benull.- Parameters:
dataSource- underlyingDataSource
-
-
Method Details
-
getSqlDataSource
Returns the underlying rawDataSource.- Returns:
- the underlying raw
DataSource
-
getSqlDialect
Returns theSqlDialectof the underlyingDataSource. This method caches the result of the analysis, so one can call it cheaply several times.- Returns:
- the
SqlDialect - Throws:
CommonAccessException- on errors during connection-handling and meta-data analysisIllegalArgumentException- if the database product name is unknown toSqlDialect
-
executeUpdate
Executes the specified SQL-update statement and returns the count of manipulated rows. (Returns the same asStatement.executeUpdate(String). The specified SQL-update statement must not benull.- Parameters:
sql- SQL-update statement to execute- Returns:
- count of manipulated rows
- Throws:
CommonAccessException- on errors during connection-handling and sql-execution
-
executeQuery
public void executeQuery(String sql, ResultReader<? extends CommonReadResultException> resultReader) throws CommonAccessException, CommonReadResultException Executes the specified SQL-select statement and calls the specifiedResultReader. The specified SQL-select statement must not benull. The specifiedResultReadermust not benull.- Parameters:
sql- SQL-select statement to executeresultReader-ResultReaderto call- Throws:
CommonAccessException- on errors during connection-handling and sql-executionCommonReadResultException- on business-errors signaled by theResultReader
-
onErrorGettingConnection
This event-method is called by all SQL-execution-methods if there is noConnectionavailable from the underlyingDataSource. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byDataSource.getConnection()sql- SQL-statement of the SQL-execution-method- Returns:
- a suitable
Exception
-
onErrorCreatingStatement
This event-method is called by all SQL-execution-methods if there is noStatementavailable from the underlyingConnection. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byConnection.createStatement()sql- SQL-statement of the SQL-execution-method- Returns:
- a suitable
Exception
-
onErrorExecutingUpdate
This event-method is called byexecuteUpdate(String)if there is any error executing the statement. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byStatement.executeUpdate(String)sql- SQL-statement of the SQL-execution-method- Returns:
- a suitable
Exception
-
onErrorExecutingQuery
This event-method is called byexecuteQuery(String, ResultReader)if there is any error executing the statement. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byStatement.executeQuery(String)sql- SQL-statement of the SQL-execution-method- Returns:
- a suitable
Exception
-
onErrorReadingResultSet
This event-method is called byexecuteQuery(String, ResultReader)if there is any error reading theResultSet. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byany of the ResultSet.get...()-methodssql- SQL-statement of the SQL-execution-method- Returns:
- a suitable
Exception
-
onErrorGettingMetaData
This event-method is called bygetSqlDialect()if there is any error getting theDatabaseMetaData. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byConnection.getMetaData()- Returns:
- a suitable
Exception
-
onErrorGettingDatabaseProductName
This event-method is called bygetSqlDialect()if there is any error getting the database product name. This method must return a suitableException.- Parameters:
t- originalThrowablethrown byDatabaseMetaData.getDatabaseProductName()- Returns:
- a suitable
Exception
-
beforeExecuteUpdate
This event-method is called immediately beforeStatement.executeUpdate(String)is called.- Parameters:
sql- SQL-statement of the SQL-execution-method
-
noteExecuteUpdateTime
This event-method is called immediately afterStatement.executeUpdate(String)is called, no matter whether the call was successful or not.- Parameters:
sql- SQL-statement of the SQL-execution-methodtimeNanoseconds- the execution-time ofStatement.executeUpdate(String)in nanoseconds, no matter whether the call was successful or notsuccess- indicates whether the call was successful or not
-
afterExecuteUpdate
This event-method is called immediately afterStatement.executeUpdate(String)was successfully called. It is called afternoteExecuteUpdateTime(String, long, boolean).- Parameters:
sql- SQL-statement of the SQL-execution-methodrowCount- count of manipulated rows
-
beforeExecuteQuery
This event-method is called immediately beforeStatement.executeQuery(String)is called.- Parameters:
sql- SQL-statement of the SQL-execution-method
-
noteExecuteQueryTime
This event-method is called immediately afterStatement.executeQuery(String)is called, no matter whether the call was successful or not.- Parameters:
sql- SQL-statement of the SQL-execution-methodtimeNanoseconds- the execution-time ofStatement.executeQuery(String)in nanoseconds, no matter whether the call was successful or notsuccess- indicates whether the call was successful or not
-
afterExecuteQuery
This event-method is called immediately afterStatement.executeQuery(String)was successfully called. It is called afternoteExecuteQueryTime(String, long, boolean).- Parameters:
sql- SQL-statement of the SQL-execution-method
-
beforeReadResult
This event-method is called immediately beforeResultReader.readResult(ResultSet)is called.- Parameters:
sql- SQL-statement of the SQL-execution-method
-
noteReadResultTime
This event-method is called immediately afterResultReader.readResult(ResultSet)is called, no matter whether the call was successful or not.- Parameters:
sql- SQL-statement of the SQL-execution-methodtimeNanoseconds- the execution-time ofResultReader.readResult(ResultSet)in nanoseconds, no matter whether the call was successful or notsuccess- indicates whether the call was successful or not
-
afterReadResult
This event-method is called immediately afterResultReader.readResult(ResultSet)was successfully called.It is called afternoteReadResultTime(String, long, boolean).- Parameters:
sql- SQL-statement of the SQL-execution-methodrowCount- count of selected rows
-