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 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 java.lang.Exception,CommonReadResultException extends java.lang.Exception> extends java.lang.ObjectDataSourceencapsulates aDataSourceand 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 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 protectedDataSource(javax.sql.DataSource dataSource)Creates a newDataSourceencapsulating the specifiedDataSource.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidafterExecuteQuery(java.lang.String sql)This event-method is called immediately afterStatement.executeQuery(String)was successfully called.protected abstract voidafterExecuteUpdate(java.lang.String sql, int rowCount)This event-method is called immediately afterStatement.executeUpdate(String)was successfully called.protected abstract voidafterReadResult(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 voidbeforeExecuteQuery(java.lang.String sql)This event-method is called immediately beforeStatement.executeQuery(String)is called.protected abstract voidbeforeExecuteUpdate(java.lang.String sql)This event-method is called immediately beforeStatement.executeUpdate(String)is called.protected abstract voidbeforeReadResult(java.lang.String sql)This event-method is called immediately beforeResultReader.readResult(ResultSet)is called.voidexecuteQuery(java.lang.String sql, ResultReader<? extends CommonReadResultException> resultReader)Executes the specified SQL-select statement and calls the specifiedResultReader.intexecuteUpdate(java.lang.String sql)Executes the specified SQL-update statement and returns the count of manipulated rows.javax.sql.DataSourcegetSqlDataSource()Returns the underlying rawDataSource.SqlDialectgetSqlDialect()Returns theSqlDialectof the underlyingDataSource.protected abstract voidnoteExecuteQueryTime(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 voidnoteExecuteUpdateTime(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 voidnoteReadResultTime(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 CommonAccessExceptiononErrorCreatingStatement(java.lang.Throwable t, java.lang.String sql)This event-method is called by all SQL-execution-methods if there is noStatementavailable from the underlyingConnection.protected abstract CommonAccessExceptiononErrorExecutingQuery(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 CommonAccessExceptiononErrorExecutingUpdate(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 CommonAccessExceptiononErrorGettingConnection(java.lang.Throwable t, java.lang.String sql)This event-method is called by all SQL-execution-methods if there is noConnectionavailable from the underlyingDataSource.protected abstract CommonAccessExceptiononErrorGettingDatabaseProductName(java.lang.Throwable t)This event-method is called bygetSqlDialect()if there is any error getting the database product name.protected abstract CommonAccessExceptiononErrorGettingMetaData(java.lang.Throwable t)This event-method is called bygetSqlDialect()if there is any error getting theDatabaseMetaData.protected abstract CommonAccessExceptiononErrorReadingResultSet(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 newDataSourceencapsulating the specifiedDataSource. The specifiedDataSourcemust 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 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 analysisjava.lang.IllegalArgumentException- if the database product name is unknown toSqlDialectCommonAccessException extends java.lang.Exception
-
executeUpdate
public int executeUpdate(java.lang.String sql) throws CommonAccessException extends java.lang.ExceptionExecutes 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.ExceptionExecutes 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 theResultReaderCommonAccessException 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 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
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 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
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- originalThrowablethrown 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- originalThrowablethrown 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- originalThrowablethrown byany of the ResultSet.get...()-methodssql- 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- originalThrowablethrown 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- originalThrowablethrown 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
-
-