openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r888100 - in /openjpa/branches/1.0.x: openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/
Date Mon, 07 Dec 2009 19:31:47 GMT
Author: mikedd
Date: Mon Dec  7 19:31:35 2009
New Revision: 888100

URL: http://svn.apache.org/viewvc?rev=888100&view=rev
Log:
OPENJPA-1350:
Add synchronization in processRegisteredClasses to prevent unknown alias error. 
Submitted by : Jody Grassel

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

Modified: openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=888100&r1=888099&r2=888100&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
(original)
+++ openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
Mon Dec  7 19:31:35 2009
@@ -34,10 +34,10 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.enhance.DynamicPersistenceCapable;
 import org.apache.openjpa.enhance.PCRegistry;
-import org.apache.openjpa.enhance.PCRegistry.RegisterClassListener;
 import org.apache.openjpa.enhance.PersistenceCapable;
-import org.apache.openjpa.enhance.DynamicPersistenceCapable;
+import org.apache.openjpa.enhance.PCRegistry.RegisterClassListener;
 import org.apache.openjpa.event.LifecycleEventManager;
 import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.Configuration;
@@ -45,10 +45,11 @@
 import org.apache.openjpa.lib.util.Closeable;
 import org.apache.openjpa.lib.util.J2DoPrivHelper;
 import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.ImplHelper;
 import org.apache.openjpa.util.InternalException;
 import org.apache.openjpa.util.MetaDataException;
 import org.apache.openjpa.util.OpenJPAId;
-import org.apache.openjpa.util.ImplHelper;
+
 import serp.util.Strings;
 
 /**
@@ -131,6 +132,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 ArrayList();
 
     // set of metadatas we're in the process of resolving
@@ -268,7 +270,7 @@
             _sourceMode |= mode;
         else
             _sourceMode &= ~mode;
-    }
+    } 
 
     /**
      * Return the metadata for the given class.
@@ -1297,6 +1299,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);
         }
     }
@@ -1317,19 +1320,50 @@
     }
 
     /**
-     * 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 getSequenceMetaData(..)

+            // and another on getMetaData(String , ClassLoader , boolean ) .
+            synchronized(this){
+                synchronized(_registered){
+                    // 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
added 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.
+     */
+    private 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;
@@ -1355,9 +1389,7 @@
             }
         }
         if (failed != null) {
-            synchronized (_registered) {
                 _registered.addAll(failed);
-            }
         }
         return reg;
     }

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java?rev=888100&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
Mon Dec  7 19:31:35 2009
@@ -0,0 +1,46 @@
+/*
+ * 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.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/MdrTestEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java?rev=888100&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetaDataRepository.java
Mon Dec  7 19:31:35 2009
@@ -0,0 +1,117 @@
+/*
+ * 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();
+            }
+        }
+    }
+}
\ No newline at end of file

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

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java?rev=888100&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
Mon Dec  7 19:31:35 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/UnloadedEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message