Class AbstractCachingSqlQueryResolver

java.lang.Object
org.hawaiiframework.sql.AbstractCachingSqlQueryResolver
All Implemented Interfaces:
SqlQueryResolver
Direct Known Subclasses:
ResourceSqlQueryResolver

public abstract class AbstractCachingSqlQueryResolver extends Object implements SqlQueryResolver
Convenient base class for SqlQueryResolver implementations. Caches sql queries once resolved: This means that sql query resolution won't be a performance problem, no matter how costly initial sql query retrieval is.

Subclasses need to implement the loadSqlQuery(java.lang.String, org.hawaiiframework.sql.AbstractCachingSqlQueryResolver.QueryHolder) template method to load the sql query.

Note this implementation is based on Spring's AbstractCachingViewResolver.

Since:
2.0.0
See Also:
  • Field Details

    • DEFAULT_CACHE_LIMIT

      public static final int DEFAULT_CACHE_LIMIT
      Default maximum number of entries for the sql query cache (1024).
      See Also:
  • Constructor Details

    • AbstractCachingSqlQueryResolver

      public AbstractCachingSqlQueryResolver()
  • Method Details

    • getCacheLimit

      public int getCacheLimit()
      Return the maximum number of entries for the sql query cache.
    • setCacheLimit

      public void setCacheLimit(int cacheLimit)
      Specify the maximum number of entries for the sql query cache. Default is 1024.
    • isCache

      public boolean isCache()
      Return if caching is enabled.
    • setCache

      public void setCache(boolean cache)
      Enable or disable caching.

      This is equivalent to setting the "cacheLimit" property to the default limit (1024) or to 0, respectively.

      Default is "true": caching is enabled. Disable this only for debugging and development.

    • setCacheUnresolved

      public void setCacheUnresolved(boolean cacheUnresolved)
      Whether a sql query name once resolved to null should be cached and automatically resolved to null subsequently.

      Default is "true": unresolved sql query names are being cached. Note that this flag only applies if the general "cache" flag is kept at its default of "true" as well.

    • getCacheKey

      protected Object getCacheKey(String sqlQueryName)
      Return the cache key for the given sql query name.

      Default is the sql query name but can be overridden in subclasses.

    • setCacheSeconds

      public void setCacheSeconds(int cacheSeconds)
      Set the number of seconds to cache a loaded query.

      After the cache seconds have expired, doRefreshQueryHolder(String, QueryHolder) will be called, so even if refreshing a once loaded query is enabled, it is up to the subclass to define the refresh mechanism.

      Note, setting this to anything other than -1 only makes sense if the queries are loaded from a source other than the classpath.

      • Default is "-1", indicating to cache forever.
      • A positive number will cache a query for the given number of seconds. This is essentially the interval between refresh checks.
      • A value of "0" will check for expiry on each query access!
    • removeFromCache

      public void removeFromCache(String sqlQueryName)
      Provides functionality to clear the cache for a certain sql query.
      Parameters:
      sqlQueryName - the sql query name for which the cached sql query (if any) needs to be removed
    • clearCache

      public void clearCache()
      Clear the entire sql query cache, removing all cached sql queries. Subsequent resolve calls will lead to loading of demanded sql queries.
    • resolveSqlQuery

      public String resolveSqlQuery(String sqlQueryName)
      Description copied from interface: SqlQueryResolver
      Resolve the given sql query by name.

      To allow for SqlQueryResolver chaining, a ViewResolver should return null if a sql query with the given name is not defined in it.

      Specified by:
      resolveSqlQuery in interface SqlQueryResolver
      Parameters:
      sqlQueryName - name of the sql query to resolve
      Returns:
      the sql query, or null if not found (optional, to allow for SqlQueryResolver chaining)
    • doRefreshQueryHolder

      protected void doRefreshQueryHolder(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
      Subclasses may override this method to implement their own expiry mechanism. The default implementation does nothing, i.e. once a query is cached it will never be updated.

      This method is only called when the current thread has a lock on the QueryHolder, so subclasses need not deal with thread-safety.

      Parameters:
      sqlQueryName - the name of the query to refresh
      queryHolder - the cached QueryHolder to check
    • loadSqlQuery

      protected abstract String loadSqlQuery(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
      Subclasses must implement this method to load the sql query. The returned sql queries will be cached by this SqlQueryResolver base class.
      Parameters:
      sqlQueryName - the name of the sql query to retrieve
      queryHolder - the QueryHolder to enrich
      Returns:
      the sql query, or null if not found (optional, to allow for SqlQueryResolver chaining)
      Throws:
      HawaiiException - if the sql query could not be resolved (typically in case of problems resolving the sql query)
      See Also: