Skip to content

Commit

Permalink
Add logging to PreparedQueryCache
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/sql-cache@695021 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Pinaki Poddar committed Sep 13, 2008
1 parent 5f1c9fd commit 36cf386
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.lib.conf.Configuration;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.lib.log.LogFactory;
import org.apache.openjpa.lib.util.Localizer;

/**
* An implementation of the cache of {@link PreparedQuery prepared queries}.
Expand All @@ -41,14 +45,16 @@
public class PreparedQueryCacheImpl implements PreparedQueryCache {
private static final String PATTERN_SEPARATOR = "\\;";
private static final String EXLUDED_BY_USER = "Excluded by user";

// Key: Query identifier
private final Map<String, PreparedQuery> _delegate;
// Key: Query identifier Value: Reason why excluded
private final Map<String, String> _uncachables;
private List<String> _exclusionPatterns;
private final QueryStatistics _stats;
private ReentrantLock _lock = new ReentrantLock();
private Log _log;
private Localizer _loc = Localizer.forPackage(PreparedQueryCacheImpl.class);

public PreparedQueryCacheImpl() {
_delegate = new HashMap<String, PreparedQuery>();
Expand Down Expand Up @@ -78,13 +84,19 @@ public boolean cache(PreparedQuery q) {
lock();
try {
String id = q.getIdentifier();
if (isCachable(id) == Boolean.FALSE)
if (isCachable(id) == Boolean.FALSE) {
if (_log.isWarnEnabled())
_log.warn(_loc.get("prepared-query-not-cachable", id));
return false;
}
String pattern = getMatchedExclusionPattern(id);
if (pattern != null) {
markUncachable(q.getIdentifier(), pattern);
return false;
}
if (_log.isTraceEnabled())
_log.trace(_loc.get("prepared-query-cache", q.getIdentifier(),
q.getDatastoreAction()));
_delegate.put(q.getIdentifier(), q);
return true;
} finally {
Expand All @@ -95,6 +107,8 @@ public boolean cache(PreparedQuery q) {
public boolean invalidate(String id) {
lock();
try {
if (_log.isTraceEnabled())
_log.trace(_loc.get("prepared-query-invalidate", id));
return _delegate.remove(id) != null;
} finally {
unlock();
Expand Down Expand Up @@ -127,11 +141,19 @@ public PreparedQuery markUncachable(String id) {
return markUncachable(id, EXLUDED_BY_USER);
}

private PreparedQuery markUncachable(String id, String pattern) {
private PreparedQuery markUncachable(String id, String reason) {
lock();
try {
if (_uncachables.get(id) != EXLUDED_BY_USER)
_uncachables.put(id, pattern);
boolean excludedByUser = _uncachables.get(id) == EXLUDED_BY_USER;
if (!excludedByUser)
_uncachables.put(id, reason);
if (_log.isInfoEnabled()) {
if (excludedByUser)
_log.info(_loc.get("prepared-query-uncache-strong", id));
else
_log.info(_loc.get("prepared-query-uncache-weak", id,
reason));
}
return _delegate.remove(id);
} finally {
unlock();
Expand Down Expand Up @@ -174,6 +196,9 @@ public void addExclusionPattern(String pattern) {
_exclusionPatterns.add(pattern);
Collection<String> invalidKeys = getMatchedKeys(pattern,
_delegate.keySet());
if (!invalidKeys.isEmpty() && _log.isInfoEnabled())
_log.info(_loc.get("prepared-query-add-pattern", pattern,
invalidKeys.size(), invalidKeys));
for (String invalidKey : invalidKeys)
markUncachable(invalidKey, pattern);
} finally {
Expand All @@ -192,8 +217,11 @@ public void removeExclusionPattern(String pattern) {
if (_exclusionPatterns == null)
return;
_exclusionPatterns.remove(pattern);
Collection<String> rebornKeys = getMatchedKeys(pattern, _uncachables);
for (String rebornKey : rebornKeys)
Collection<String> reborns = getMatchedKeys(pattern, _uncachables);
if (!reborns.isEmpty() && _log.isInfoEnabled())
_log.info(_loc.get("prepared-query-remove-pattern", pattern,
reborns.size(), reborns));
for (String rebornKey : reborns)
_uncachables.remove(rebornKey);
} finally {
unlock();
Expand Down Expand Up @@ -268,6 +296,7 @@ boolean matches(String pattern, String target) {
// Configurable contract
//-------------------------------------------------------
public void setConfiguration(Configuration conf) {
_log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
}

public void startConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,17 @@ gap-query-param: Parameter {1} for query "{0}" exceeds the number of {2} \
bound parameters with following values "{3}". This can happen if you have \
declared but missed to bind values for one or more parameters.
query-execution-error: Failed to execute query "{0}". Check the query syntax \
for correctness. See nested exception for details.
for correctness. See nested exception for details.
prepared-query-cache: Adding query "{0}" to prepared query cache. The target \
database query is "{1}".
prepared-query-not-cachable: Query "{0}" can not be cached.
prepared-query-invalidate: Prepared Query "{0}" is invalidated
prepared-query-uncache-strong: Query "{0}" is marked permanently not to be \
cached.
prepared-query-uncache-weak: Query "{0}" is marked not to be cached because it \
matched exclusion pattern "{1}". This query can be cached if the exclusion \
pattern is removed later.
prepared-query-add-pattern: Added exclusion pattern "{0}" which has removed \
following {1} prepared queries from the cache: "{2}"
prepared-query-remove-pattern: Removed exclusion pattern "{0}" which has made \
following {1} queries again possible to be cached: "{2}"

0 comments on commit 36cf386

Please sign in to comment.