openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r835220 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Date Thu, 12 Nov 2009 04:14:57 GMT
Author: ppoddar
Date: Thu Nov 12 04:14:55 2009
New Revision: 835220

URL: http://svn.apache.org/viewvc?rev=835220&view=rev
Log:
OPENJPA-1381: Ignore cached target query when lock mode is set

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=835220&r1=835219&r2=835220&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Thu Nov 12 04:14:55 2009
@@ -26,6 +26,7 @@
 import java.util.Properties;
 
 import javax.persistence.EntityManager;
+import javax.persistence.LockModeType;
 import javax.persistence.Query;
 
 import junit.framework.TestCase;
@@ -738,6 +739,24 @@
         }
     }
     
+    public void testPreparedQueryIgnoredWhenLockModeIsSet() {
+        String jpql = "select p from Author p";
+        EntityManager em = emf.createEntityManager();
+        
+        Query q1 = em.createQuery(jpql);
+        assertEquals(JPQLParser.LANG_JPQL, OpenJPAPersistence.cast(q1).getLanguage());
+        List<Author> authors1 = q1.getResultList();
+        
+        // do the same thing again, this time query should be cached
+        em.getTransaction().begin();
+        Query q2 = em.createQuery(jpql);
+        assertEquals(QueryLanguages.LANG_PREPARED_SQL, OpenJPAPersistence.cast(q2).getLanguage());
+        q2.setLockMode(LockModeType.OPTIMISTIC);
+        assertEquals(JPQLParser.LANG_JPQL, OpenJPAPersistence.cast(q2).getLanguage());
+        List<Author> authors2 = q2.getResultList();
+        em.getTransaction().rollback();
+    }
+    
     
     PreparedQueryCache getPreparedQueryCache() {
         return emf.getConfiguration().getQuerySQLCacheInstance();

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=835220&r1=835219&r2=835220&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Thu Nov 12 04:14:55 2009
@@ -372,8 +372,12 @@
 		return QueryLanguages.LANG_SQL.equals(getLanguage());
 	}
 
+	/**
+	 * Asserts that this query is a JPQL or Criteria Query.
+	 */
 	void assertJPQLOrCriteriaQuery() {
-        if (!(JPQLParser.LANG_JPQL.equals(getLanguage()) || CriteriaBuilderImpl.LANG_CRITERIA.equals(getLanguage())))
{
+        String language = getLanguage();
+        if (!(JPQLParser.LANG_JPQL.equals(language) || CriteriaBuilderImpl.LANG_CRITERIA.equals(language)))
{
             throw new IllegalStateException(_loc.get("not-jpql-or-criteria-query").getMessage());
         }
 	}
@@ -392,7 +396,16 @@
         return _fetch.getReadLockMode();
     }
 
+    /**
+     * Sets lock mode on the given query.
+     * If the target query has been prepared and cached, then ignores the cached version.
+     * @see #ignorePreparedQuery()
+     */
     public TypedQuery<X> setLockMode(LockModeType lockMode) {
+        String language = getLanguage();
+        if (QueryLanguages.LANG_PREPARED_SQL.equals(language)) {
+            ignorePreparedQuery();
+        }
         assertJPQLOrCriteriaQuery();
        _fetch.setReadLockMode(lockMode);
        return this;



Mime
View raw message