You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2010/02/25 22:44:11 UTC

svn commit: r916469 - in /openjpa/branches/1.3.x: openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/

Author: dwoods
Date: Thu Feb 25 21:44:11 2010
New Revision: 916469

URL: http://svn.apache.org/viewvc?rev=916469&view=rev
Log:
OPENJPA-1350 Race condition in the MetaDataRepository.  Merged in r911174 and r911175 from 1.2.x branch.

Added:
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java   (with props)
Modified:
    openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java

Modified: openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=916469&r1=916468&r2=916469&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java (original)
+++ openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java Thu Feb 25 21:44:11 2010
@@ -133,6 +133,7 @@
 
     // we buffer up any classes that register themselves to prevent
     // reentrancy errors if classes register during a current parse (common)
+    private boolean _registeredEmpty = true;
     private final Collection _registered = new HashSet();
 
     // set of metadatas we're in the process of resolving
@@ -1361,6 +1362,7 @@
         // buffer registered classes until an oid metadata request is made,
         // at which point we'll parse everything in the buffer
         synchronized (_registered) {
+            _registeredEmpty = false;
             _registered.add(cls);
         }
     }
@@ -1381,19 +1383,48 @@
     }
 
     /**
-     * Updates our datastructures with the latest registered classes.
+     * Updates our datastructures with the latest registered classes.  This method will only block
+     * when there are class registrations waiting to be processed.
+     * 
+     * @return the list of classes that was processed due to this method call.
      */
     Class[] processRegisteredClasses(ClassLoader envLoader) {
+        Class[] res = EMPTY_CLASSES;
+        if (_registeredEmpty == false) {
+            // The reason that we're locking on this and _registered is that this
+            // class has locking semantics such that we always need to lock 'this', then [x].
+            // This method can result in processRegisteredClass(..) being called and if we didn't
+            // lock on 'this' we could cause a deadlock.  ie: One thread coming in on getSequeneceMetaData(..)
+            // and another on getMetaData(String, Classloader, boolean).
+            synchronized (this) {
+                // Check again, it is possible another thread already processed.
+                if (_registeredEmpty == false) {
+                    res = processRegisteredClassesInternal(envLoader);
+                    if (_registered.size() == 0) {
+                        // It is possible that we failed to register a class and it was sadded back into
+                        // our _registered class list to try again later.  @see
+                        // OpenJPAConfiguration#getRetryClassRegistration
+                        _registeredEmpty = true;
+                    }
+                }
+            }
+        }
+        return res;
+    }
+
+    /**
+     * Private worker method that processes the registered classes list.  No locking is performed in this
+     * method as the caller needs to hold a lock on _registered.
+     */
+    Class[] processRegisteredClassesInternal(ClassLoader envLoader) {
+        // Probably overkill
         if (_registered.isEmpty())
             return EMPTY_CLASSES;
 
         // copy into new collection to avoid concurrent mod errors on reentrant
         // registrations
-        Class[] reg;
-        synchronized (_registered) {
-            reg = (Class[]) _registered.toArray(new Class[_registered.size()]);
-            _registered.clear();
-        }
+        Class[] reg = (Class[]) _registered.toArray(new Class[_registered.size()]);
+        _registered.clear();
 
         Collection pcNames = getPersistentTypeNames(false, envLoader);
         Collection failed = null;
@@ -1419,9 +1450,7 @@
             }
         }
         if (failed != null) {
-            synchronized (_registered) {
-                _registered.addAll(failed);
-            }
+            _registered.addAll(failed);
         }
         return reg;
     }

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java?rev=916469&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java Thu Feb 25 21:44:11 2010
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.meta;
+
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+
+@Entity
+@NamedQueries( 
+    { 
+        @NamedQuery(name = "query", query = "SELECT p FROM MdrTestEntity p")
+    }
+)
+public class MdrTestEntity {
+    @Id
+    int id;
+
+    @ManyToOne(cascade = CascadeType.MERGE)
+    MdrTestEntity manyToOne;
+
+    @OneToMany
+    List<MdrTestEntity> oneToMany;
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java?rev=916469&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java Thu Feb 25 21:44:11 2010
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.meta;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.openjpa.meta.MetaDataRepository;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestMetaDataRepository extends SingleEMFTestCase {
+    private final int threads = 20;
+    public static boolean UnloadedEntityLoaded = false;
+    @Override
+    public void setUp() throws Exception {
+        // Need to enable trace to slow down first thread to force timing condition.
+        setUp(MdrTestEntity.class,"openjpa.Log","MetaData=trace");
+    }
+
+    public void testEntityLoading() throws Exception {
+        EntityManager em = null;
+        try{
+            emf.close();
+            String unloadedClass = "org.apache.openjpa.persistence.meta.UnloadedEntity";
+            setUp("openjpa.MetaDataFactory","jpa(Types="+unloadedClass+")");
+            assertFalse(UnloadedEntityLoaded);
+            em = emf.createEntityManager();
+            assertTrue(UnloadedEntityLoaded);
+        }finally{
+            em.close();
+        }
+    }
+
+    /**
+     * This method tests a timing window where more than one thread requests MetaData using an alias
+     * at the same time. All threads should get data back and no threads should receive an
+     * exception.
+     */
+    public void testMultiThreadGetMetaDataAlias() throws Exception {
+        try {
+            
+            List<Worker> workers = new ArrayList<Worker>();
+            Set<Exception> exceptions = new HashSet<Exception>();
+            for (int i = 0; i < threads; i++) {
+                Worker w = new Worker(emf);
+                workers.add(w);
+            }
+            for (Worker w : workers) {
+                w.start();
+            }
+            for (Worker w : workers) {
+                w.join();
+                Exception e = w.getException();
+                if (e != null) {
+                    exceptions.add(w.getException());
+                }
+            }
+            assertTrue("Caught "  + exceptions.toString(), exceptions.size() == 0);
+        } finally {
+            if (emf != null) {
+                emf.close();
+            }
+        }
+    }
+
+    class Worker extends Thread {
+        OpenJPAEntityManagerFactorySPI emf;
+        OpenJPAEntityManagerSPI em;
+        MetaDataRepository repo;
+        Exception ex;
+
+        Worker(EntityManagerFactory e) {
+            emf = (OpenJPAEntityManagerFactorySPI) e;
+            em = emf.createEntityManager();
+            repo = em.getConfiguration().getMetaDataRepositoryInstance();
+        }
+
+        Exception getException() {
+            return ex;
+        }
+
+        @Override
+        public void run() {
+            try {
+                repo.getMetaData("MdrTestEntity", Thread.currentThread().getContextClassLoader(), true);
+            } catch (Exception e) {
+                ex = e;
+                e.printStackTrace();
+            } finally {
+                em.close();
+            }
+        }
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java?rev=916469&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java Thu Feb 25 21:44:11 2010
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.meta;
+
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class UnloadedEntity {
+    // This block calls back to the testcase so we can detect if it was loaded.
+    static { 
+        TestMetaDataRepository.UnloadedEntityLoaded = true;
+    }
+    @Basic
+    private String implClassAName;
+    
+    @Id 
+    @GeneratedValue
+    private int id;
+    
+    @Basic
+    private int intFieldSup;
+    
+    public void setImplClassAName(String implClassAName) {
+        this.implClassAName = implClassAName;
+    }
+
+    public String getImplClassAName() {
+        return implClassAName;
+    }
+    
+    public String toString() {
+        return super.toString() + ";implClassAName=" + implClassAName + 
+            ";intFieldSup=" + intFieldSup+ ";id="+id;
+    }
+
+    public int getId() {
+        return this.id;
+    }
+
+    public int getIntFieldSup() {
+        return intFieldSup;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public void setIntFieldSup(int i) {
+        this.intFieldSup = i;
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native