From 13e3c48caea0c7c8125b72f398f6443ce231902f Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Thu, 18 Mar 2010 23:52:33 +0000 Subject: [PATCH] OPENJPA-1580: disable query cache when pagination is involved. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@925048 13f79535-47bb-0310-9956-ffa450edef68 --- .../jdbc/kernel/PreparedQueryImpl.java | 16 +++++++++++++-- .../openjpa/jdbc/kernel/localizer.properties | 4 +++- .../jdbc/sqlcache/TestPreparedQueryCache.java | 20 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java index 67fabc80a..558e8569e 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java @@ -178,10 +178,14 @@ public class PreparedQueryImpl implements PreparedQuery { SQLBuffer buffer = selector.getSQL(); if (buffer == null) return new PreparedQueryCacheImpl.StrongExclusion(_id, _loc.get("exclude-no-sql", _id).getMessage());; - boolean useFieldStrategy = isUsingFieldStrategy(); - if (useFieldStrategy) + if (isUsingFieldStrategy()) return new PreparedQueryCacheImpl.StrongExclusion(_id, _loc.get("exclude-user-strategy", _id).getMessage());; + + if (isPaginated()) + return new PreparedQueryCacheImpl.StrongExclusion(_id, + _loc.get("exclude-pagination", _id).getMessage());; + setTargetQuery(buffer.getSQL()); setParameters(buffer.getParameters()); setUserParameterPositions(buffer.getUserParameters()); @@ -259,6 +263,14 @@ public class PreparedQueryImpl implements PreparedQuery { return false; } + private boolean isPaginated() { + if (select instanceof SelectImpl) { + if (((SelectImpl)select).getStartIndex() != 0 || + ((SelectImpl)select).getEndIndex() != Long.MAX_VALUE) + return true; + } + return false; + } private boolean isUsingFieldStrategy() { for (int i = 0; i < _exps.length; i++) { if (isUsingFieldStrategy(_exps[i])) { diff --git a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties index e925ed7b9..5bb99f68a 100644 --- a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties +++ b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties @@ -168,4 +168,6 @@ exclude-not-executor: Query "{0}" is not cached because it was not executed on a exclude-externalized-param: Query "{0}" is not cached because some parameterized \ field values are externalized. exclude-user-strategy: This query "{0}" is not cached because some parameterized \ - field value depends on user-defined field strategy. \ No newline at end of file + field value depends on user-defined field strategy. +exclude-pagination: This query "{0}" involves pagination and is not cached. + \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java index 59b88a1f6..c65b0fa2c 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java @@ -974,7 +974,27 @@ public class TestPreparedQueryCache extends TestCase { em.getTransaction().rollback(); } + public void testRangeIsExcluded() { + List l = null; + l = getAllCompaniesPaged(0, 1); + assertEquals(1, l.size()); + assertEquals("acme.org", l.get(0).getName()); + l = getAllCompaniesPaged(1, 1); + assertEquals(1, l.size()); + assertEquals("BEA", l.get(0).getName()); + l = getAllCompaniesPaged(2, 1); + assertEquals(1, l.size()); + assertEquals("IBM", l.get(0).getName()); + } + + public List getAllCompaniesPaged(int start, int max) { + EntityManager em = emf.createEntityManager(); + Query q = em.createQuery("select p from Company p order by p.name"); + q.setFirstResult(start); + q.setMaxResults(max); + return (List) q.getResultList(); + } PreparedQueryCache getPreparedQueryCache() { return emf.getConfiguration().getQuerySQLCacheInstance();