Class CachedStatement

java.lang.Object
org.apache.sis.metadata.sql.CachedStatement
All Implemented Interfaces:
AutoCloseable

final class CachedStatement extends Object implements 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 the MetadataSource which contain this entry.

Closing

While this class implements AutoCloseable, 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 Details

    • LOGGER

      static final Logger LOGGER
      Where to log warnings.
    • type

      final Class<?> type
      The interface for which the prepared statement has been created.
    • identifier

      private String identifier
      The identifier (usually the primary key) for current results. If the record to fetch does not have the same identifier, then the results will need to be closed and reconstructed.
    • statement

      private final PreparedStatement statement
      The statement associated with this entry. The SQL query depends on the type, which cannot be changed, and the identifier, which can be changed at any time. The first parameter of the statement shall be the identifier.
    • results

      private ResultSet results
      The results of last call to PreparedStatement.executeQuery(), or null if not yet determined.
    • expireTime

      long expireTime
      The expiration time of this result, in nanoseconds as given by System.nanoTime(). This is read and updated by MetadataSource only.
    • logFilter

      private final Filter 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

      final Object getValue(String id, String attribute) throws SQLException, MetadataStoreException
      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

      private void closeResultSet() throws SQLException
      Closes the current ResultSet. 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

      public void close() throws SQLException
      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 by MetadataSource.close() instead.

      Specified by:
      close in interface AutoCloseable
      Throws:
      SQLException - if an error occurred while closing the statement.
    • warning

      private void warning(Class<?> source, String method, LogRecord record)
      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.