Package com.saperion.common.sql
Class DataSource<CommonAccessException extends java.lang.Exception,CommonReadResultException extends java.lang.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 java.lang.Exception,CommonReadResultException extends java.lang.Exception> extends java.lang.Object
DataSource
encapsulates aDataSource
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 isSimpleDataSource
. 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:
SqlDialect
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DataSource(javax.sql.DataSource dataSource)
Creates a newDataSource
encapsulating the specifiedDataSource
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
afterExecuteQuery(java.lang.String sql)
This event-method is called immediately afterStatement.executeQuery(String)
was successfully called.protected abstract void
afterExecuteUpdate(java.lang.String sql, int rowCount)
This event-method is called immediately afterStatement.executeUpdate(String)
was successfully called.protected abstract void
afterReadResult(java.lang.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(java.lang.String sql)
This event-method is called immediately beforeStatement.executeQuery(String)
is called.protected abstract void
beforeExecuteUpdate(java.lang.String sql)
This event-method is called immediately beforeStatement.executeUpdate(String)
is called.protected abstract void
beforeReadResult(java.lang.String sql)
This event-method is called immediately beforeResultReader.readResult(ResultSet)
is called.void
executeQuery(java.lang.String sql, ResultReader<? extends CommonReadResultException> resultReader)
Executes the specified SQL-select statement and calls the specifiedResultReader
.int
executeUpdate(java.lang.String sql)
Executes the specified SQL-update statement and returns the count of manipulated rows.javax.sql.DataSource
getSqlDataSource()
Returns the underlying rawDataSource
.SqlDialect
getSqlDialect()
Returns theSqlDialect
of the underlyingDataSource
.protected abstract void
noteExecuteQueryTime(java.lang.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(java.lang.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(java.lang.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(java.lang.Throwable t, java.lang.String sql)
This event-method is called by all SQL-execution-methods if there is noStatement
available from the underlyingConnection
.protected abstract CommonAccessException
onErrorExecutingQuery(java.lang.Throwable t, java.lang.String sql)
This event-method is called byexecuteQuery(String, ResultReader)
if there is any error executing the statement.protected abstract CommonAccessException
onErrorExecutingUpdate(java.lang.Throwable t, java.lang.String sql)
This event-method is called byexecuteUpdate(String)
if there is any error executing the statement.protected abstract CommonAccessException
onErrorGettingConnection(java.lang.Throwable t, java.lang.String sql)
This event-method is called by all SQL-execution-methods if there is noConnection
available from the underlyingDataSource
.protected abstract CommonAccessException
onErrorGettingDatabaseProductName(java.lang.Throwable t)
This event-method is called bygetSqlDialect()
if there is any error getting the database product name.protected abstract CommonAccessException
onErrorGettingMetaData(java.lang.Throwable t)
This event-method is called bygetSqlDialect()
if there is any error getting theDatabaseMetaData
.protected abstract CommonAccessException
onErrorReadingResultSet(java.lang.Throwable t, java.lang.String sql)
This event-method is called byexecuteQuery(String, ResultReader)
if there is any error reading theResultSet
.
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
-
-
Constructor Detail
-
DataSource
protected DataSource(javax.sql.DataSource dataSource)
Creates a newDataSource
encapsulating the specifiedDataSource
. The specifiedDataSource
must not benull
.- Parameters:
dataSource
- underlyingDataSource
-
-
Method Detail
-
getSqlDataSource
public javax.sql.DataSource getSqlDataSource()
Returns the underlying rawDataSource
.- Returns:
- the underlying raw
DataSource
-
getSqlDialect
public SqlDialect getSqlDialect() throws CommonAccessException extends java.lang.Exception
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 analysisjava.lang.IllegalArgumentException
- if the database product name is unknown toSqlDialect
CommonAccessException extends java.lang.Exception
-
executeUpdate
public int executeUpdate(java.lang.String sql) throws CommonAccessException extends java.lang.Exception
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-executionCommonAccessException extends java.lang.Exception
-
executeQuery
public void executeQuery(java.lang.String sql, ResultReader<? extends CommonReadResultException> resultReader) throws CommonAccessException extends java.lang.Exception, CommonReadResultException extends java.lang.Exception
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
CommonAccessException extends java.lang.Exception
-
onErrorGettingConnection
protected abstract CommonAccessException onErrorGettingConnection(java.lang.Throwable t, java.lang.String sql)
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
protected abstract CommonAccessException onErrorCreatingStatement(java.lang.Throwable t, java.lang.String sql)
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
protected abstract CommonAccessException onErrorExecutingUpdate(java.lang.Throwable t, java.lang.String sql)
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
protected abstract CommonAccessException onErrorExecutingQuery(java.lang.Throwable t, java.lang.String sql)
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
protected abstract CommonAccessException onErrorReadingResultSet(java.lang.Throwable t, java.lang.String sql)
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
protected abstract CommonAccessException onErrorGettingMetaData(java.lang.Throwable t)
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
protected abstract CommonAccessException onErrorGettingDatabaseProductName(java.lang.Throwable t)
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
protected abstract void beforeExecuteUpdate(java.lang.String sql)
This event-method is called immediately beforeStatement.executeUpdate(String)
is called.- Parameters:
sql
- SQL-statement of the SQL-execution-method
-
noteExecuteUpdateTime
protected abstract void noteExecuteUpdateTime(java.lang.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.- 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
protected abstract void afterExecuteUpdate(java.lang.String sql, int rowCount)
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
protected abstract void beforeExecuteQuery(java.lang.String sql)
This event-method is called immediately beforeStatement.executeQuery(String)
is called.- Parameters:
sql
- SQL-statement of the SQL-execution-method
-
noteExecuteQueryTime
protected abstract void noteExecuteQueryTime(java.lang.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.- 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
protected abstract void afterExecuteQuery(java.lang.String sql)
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
protected abstract void beforeReadResult(java.lang.String sql)
This event-method is called immediately beforeResultReader.readResult(ResultSet)
is called.- Parameters:
sql
- SQL-statement of the SQL-execution-method
-
noteReadResultTime
protected abstract void noteReadResultTime(java.lang.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.- 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
protected abstract void afterReadResult(java.lang.String sql, int rowCount)
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
-
-