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 providedExceptions
during connection-handling and sql-executionCommonReadResultException
- common type of all usedResultReader
s 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 Exception
s
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 IllegalStateException
s 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
ConstructorsModifierConstructorDescriptionprotected
DataSource
(DataSource dataSource) Creates a newDataSource
encapsulating the specifiedDataSource
. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
afterExecuteQuery
(String sql) This event-method is called immediately afterStatement.executeQuery(String)
was successfully called.protected abstract void
afterExecuteUpdate
(String sql, int rowCount) This event-method is called immediately afterStatement.executeUpdate(String)
was successfully called.protected abstract void
afterReadResult
(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 void
beforeExecuteQuery
(String sql) This event-method is called immediately beforeStatement.executeQuery(String)
is called.protected abstract void
This event-method is called immediately beforeStatement.executeUpdate(String)
is called.protected abstract void
beforeReadResult
(String sql) This event-method is called immediately beforeResultReader.readResult(ResultSet)
is called.void
executeQuery
(String sql, ResultReader<? extends CommonReadResultException> resultReader) Executes the specified SQL-select statement and calls the specifiedResultReader
.int
executeUpdate
(String sql) Executes the specified SQL-update statement and returns the count of manipulated rows.Returns the underlying rawDataSource
.Returns theSqlDialect
of the underlyingDataSource
.protected abstract void
noteExecuteQueryTime
(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 void
noteExecuteUpdateTime
(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 void
noteReadResultTime
(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 CommonAccessException
onErrorCreatingStatement
(Throwable t, String sql) This event-method is called by all SQL-execution-methods if there is noStatement
available from the underlyingConnection
.protected abstract CommonAccessException
onErrorExecutingQuery
(Throwable t, String sql) This event-method is called byexecuteQuery(String, ResultReader)
if there is any error executing the statement.protected abstract CommonAccessException
onErrorExecutingUpdate
(Throwable t, String sql) This event-method is called byexecuteUpdate(String)
if there is any error executing the statement.protected abstract CommonAccessException
onErrorGettingConnection
(Throwable t, String sql) This event-method is called by all SQL-execution-methods if there is noConnection
available from the underlyingDataSource
.protected abstract CommonAccessException
This event-method is called bygetSqlDialect()
if there is any error getting the database product name.protected abstract CommonAccessException
This event-method is called bygetSqlDialect()
if there is any error getting theDatabaseMetaData
.protected abstract CommonAccessException
onErrorReadingResultSet
(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 newDataSource
encapsulating the specifiedDataSource
. The specifiedDataSource
must not benull
.- Parameters:
dataSource
- underlyingDataSource
-
-
Method Details
-
getSqlDataSource
Returns the underlying rawDataSource
.- Returns:
- the underlying raw
DataSource
-
getSqlDialect
Returns theSqlDialect
of 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 specifiedResultReader
must not benull
.- Parameters:
sql
- SQL-select statement to executeresultReader
-ResultReader
to 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 noConnection
available from the underlyingDataSource
. This method must return a suitableException
.- Parameters:
t
- originalThrowable
thrown 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 noStatement
available from the underlyingConnection
. This method must return a suitableException
.- Parameters:
t
- originalThrowable
thrown 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
- originalThrowable
thrown 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
- originalThrowable
thrown 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
- originalThrowable
thrown byany of the ResultSet.get...()-methods
sql
- 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
- originalThrowable
thrown 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
- originalThrowable
thrown 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
-