Class AbstractCachingSqlQueryResolver
- All Implemented Interfaces:
SqlQueryResolver
- Direct Known Subclasses:
ResourceSqlQueryResolver
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
.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
QueryHolder for caching. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default maximum number of entries for the sql query cache (1024
). -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clear the entire sql query cache, removing all cached sql queries.protected void
doRefreshQueryHolder
(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder) Subclasses may override this method to implement their own expiry mechanism.protected Object
getCacheKey
(String sqlQueryName) Return the cache key for the given sql query name.int
Return the maximum number of entries for the sql query cache.boolean
isCache()
Return if caching is enabled.protected abstract String
loadSqlQuery
(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder) Subclasses must implement this method to load the sql query.void
removeFromCache
(String sqlQueryName) Provides functionality to clear the cache for a certain sql query.resolveSqlQuery
(String sqlQueryName) Resolve the given sql query by name.void
setCache
(boolean cache) Enable or disable caching.void
setCacheLimit
(int cacheLimit) Specify the maximum number of entries for the sql query cache.void
setCacheSeconds
(int cacheSeconds) Set the number of seconds to cache a loaded query.void
setCacheUnresolved
(boolean cacheUnresolved) Whether a sql query name once resolved tonull
should be cached and automatically resolved tonull
subsequently.
-
Field Details
-
DEFAULT_CACHE_LIMIT
public static final int DEFAULT_CACHE_LIMITDefault 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 tonull
should be cached and automatically resolved tonull
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
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
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
Description copied from interface:SqlQueryResolver
Resolve the given sql query by name.To allow for
SqlQueryResolver
chaining, aViewResolver
should returnnull
if a sql query with the given name is not defined in it.- Specified by:
resolveSqlQuery
in interfaceSqlQueryResolver
- Parameters:
sqlQueryName
- name of the sql query to resolve- Returns:
- the sql query, or
null
if not found (optional, to allow forSqlQueryResolver
chaining) - Throws:
HawaiiException
- if the sql query could not be resolved (typically in case of problems resolving the sql query)
-
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 refreshqueryHolder
- the cached QueryHolder to check
-
loadSqlQuery
protected abstract String loadSqlQuery(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder) throws HawaiiException Subclasses must implement this method to load the sql query. The returned sql queries will be cached by thisSqlQueryResolver
base class.- Parameters:
sqlQueryName
- the name of the sql query to retrievequeryHolder
- the QueryHolder to enrich- Returns:
- the sql query, or
null
if not found (optional, to allow forSqlQueryResolver
chaining) - Throws:
HawaiiException
- if the sql query could not be resolved (typically in case of problems resolving the sql query)- See Also:
-