openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r901845 - 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/EntityManagerImpl.java
Date Thu, 21 Jan 2010 20:17:51 GMT
Author: ppoddar
Date: Thu Jan 21 20:17:50 2010
New Revision: 901845

URL: http://svn.apache.org/viewvc?rev=901845&view=rev
Log:
OPENJPA-1477: Do not use cached PreparedQuery unless it is initialized

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/EntityManagerImpl.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=901845&r1=901844&r2=901845&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 Jan 21 20:17:50 2010
@@ -41,6 +41,7 @@
 import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
 import org.apache.openjpa.lib.jdbc.JDBCEvent;
 import org.apache.openjpa.lib.jdbc.JDBCListener;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
@@ -780,6 +781,32 @@
         em.getTransaction().rollback();
     }
     
+    public void testMultithreadedAccess() {
+        OpenJPAEntityManager em1 = emf.createEntityManager();
+        String jpql = "select p from Author p where p.name=:name";
+        int N = 5;
+        Thread[] threads =  new Thread[N];
+        QueryThread[] qts = new QueryThread[N];
+        for (int i = 0; i < N; i++) {
+            OpenJPAEntityManager emt = emf.createEntityManager();
+            qts[i] = new QueryThread(emt, jpql);
+            threads[i] = new Thread(qts[i]);
+            threads[i].setDaemon(true);
+        }
+        for (Thread t : threads) {
+            t.start(); 
+        }
+        for (int i = 0; i < N; i++) {
+            try {
+              threads[i].join();
+              assertFalse(qts[i].isFailed());
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+                fail();
+            }
+        }
+    }
+    
     
     PreparedQueryCache getPreparedQueryCache() {
         return emf.getConfiguration().getQuerySQLCacheInstance();
@@ -967,5 +994,36 @@
             return new ArrayList<String>(sqls);
         }
     }
+    
+    public static class QueryThread implements Runnable {
+        private final OpenJPAEntityManager em;
+        private final String jpql;
+        private boolean failed = false;
+        public QueryThread(OpenJPAEntityManager em, String jpql) {
+            super();
+            this.em = em;
+            this.jpql = jpql;
+        }
+        
+        public void run() {
+            try {
+                for (int i = 0; i < 10 && !failed; i++) {
+                    OpenJPAQuery q = em.createQuery(jpql);
+                    q.setParameter("name", "Author-"+i);
+                    q.getResultList();
+                    if (i > 1) 
+                        assertEquals(QueryLanguages.LANG_PREPARED_SQL, q.getLanguage());
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+                failed = true;
+            }
+        }
+        
+        public boolean isFailed() {
+            return failed;
+        }
+        
+    }
 
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?rev=901845&r1=901844&r2=901845&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
Thu Jan 21 20:17:50 2010
@@ -963,7 +963,7 @@
             String qid = query;
             PreparedQuery pq = JPQLParser.LANG_JPQL.equals(language)
                 ? getPreparedQuery(qid) : null;
-            org.apache.openjpa.kernel.Query q = (pq == null)
+            org.apache.openjpa.kernel.Query q = (pq == null || !pq.isInitialized())
                 ? _broker.newQuery(language, query)
                 : _broker.newQuery(pq.getLanguage(), pq);
             // have to validate JPQL according to spec
@@ -1003,7 +1003,7 @@
             
             PreparedQuery pq = JPQLParser.LANG_JPQL.equals(meta.getLanguage())
                 ? getPreparedQuery(qid) : null;
-            org.apache.openjpa.kernel.Query del = (pq == null)
+            org.apache.openjpa.kernel.Query del = (pq == null || !pq.isInitialized())
                 ? _broker.newQuery(meta.getLanguage(), meta.getQueryString())
                 : _broker.newQuery(pq.getLanguage(), pq);
             



Mime
View raw message