Package org.apache.sis.metadata.sql
Class CachedStatement
java.lang.Object
org.apache.sis.metadata.sql.CachedStatement
- All Implemented Interfaces:
AutoCloseable
The result of a query for metadata attributes. This object prepares a statement
only once for a given table, until a certain period of inactivity is elapsed. When a particular record in the
table is fetched, the
ResultSet
is automatically constructed. If many attributes are fetched consecutively
for the same record, then the same ResultSet
is reused.
Synchronization
This class is not thread-safe. Callers must perform their own synchronization in such a way that only one query is executed on the same connection (JDBC connections cannot be assumed thread-safe). The synchronization lock shall be theMetadataSource
which contain this entry.
Closing
While this class implementsAutoCloseable
, it should not be used in a try-finally block.
This is because CachedStatement
is typically closed by a different thread than the one that created
the CachedStatement
instance. This object is closed by a background thread of MetadataSource
.- Since:
- 0.8
- Version:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) long
The expiration time of this result, in nanoseconds as given bySystem.nanoTime()
.private String
The identifier (usually the primary key) for current results.private final Filter
Where to report the warnings before to eventually log them.(package private) static final Logger
Where to log warnings.private ResultSet
The results of last call toPreparedStatement.executeQuery()
, ornull
if not yet determined.private final PreparedStatement
The statement associated with this entry.(package private) final Class<?>
The interface for which the prepared statement has been created. -
Constructor Summary
ConstructorsConstructorDescriptionCachedStatement
(Class<?> type, PreparedStatement statement, Filter logFilter) Constructs a metadata result from the specified connection. -
Method Summary
-
Field Details
-
LOGGER
Where to log warnings. -
type
The interface for which the prepared statement has been created. -
identifier
The identifier (usually the primary key) for current results. If the record to fetch does not have the same identifier, then theresults
will need to be closed and reconstructed. -
statement
The statement associated with this entry. The SQL query depends on thetype
, which cannot be changed, and theidentifier
, which can be changed at any time. The first parameter of the statement shall be the identifier. -
results
The results of last call toPreparedStatement.executeQuery()
, ornull
if not yet determined. -
expireTime
long expireTimeThe expiration time of this result, in nanoseconds as given bySystem.nanoTime()
. This is read and updated byMetadataSource
only. -
logFilter
Where to report the warnings before to eventually log them.
-
-
Constructor Details
-
CachedStatement
CachedStatement(Class<?> type, PreparedStatement statement, Filter logFilter) Constructs a metadata result from the specified connection.- Parameters:
type
- the GeoAPI interface to implement.statement
- the prepared statement.logFilter
- where to report the warnings.
-
-
Method Details
-
getValue
Returns the attribute value in the given column for the given record.- Parameters:
id
- the object identifier, usually the primary key value.attribute
- the column name of the attribute to fetch.- Returns:
- the value of the requested attribute for the row identified by the given key.
- Throws:
SQLException
- if an SQL operation failed.MetadataStoreException
- if no record has been found for the given key.
-
closeResultSet
Closes the currentResultSet
. Before doing so, we make an opportunist check for duplicated values in the table. If a duplicate is found, a warning is logged. The log message pretends to be emitted by the interface constructor, which does not exist. But this is the closest we can get from a public API.- Throws:
SQLException
-
close
Closes the statement and free all resources. After this method has been invoked, this object cannot be used anymore.This method is not invoked by the method or thread that created this
CachedStatement
instance. This method is invoked byMetadataSource.close()
instead.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
- if an error occurred while closing the statement.
-
warning
Reports a warning.- Parameters:
source
- the class to report as the warning emitter.method
- the method to report as the warning emitter.record
- the warning to report.
-