Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-dev] bug-248780.patch

Hello :

Fix for Bug#248780 is attached as "bug-248780.patch" file. Please do review this file .

Thank You

Regards
Darani



Index: jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/advanced/CacheImplJUnitTest.java
===================================================================
--- jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/advanced/CacheImplJUnitTest.java	(revision 0)
+++ jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/advanced/CacheImplJUnitTest.java	(revision 0)
@@ -0,0 +1,196 @@
+/*******************************************************************************
+* Copyright (c)  2008, Sun Microsystems, Inc. All rights reserved.
+* This program and the accompanying materials are made available under the
+* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+* which accompanies this distribution.
+* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+* and the Eclipse Distribution License is available at
+* http://www.eclipse.org/org/documents/edl-v10.php.
+*
+* Contributors:
+*     DaraniY  = 1.0 - Initialize contribution
+******************************************************************************/
+package org.eclipse.persistence.testing.tests.jpa.advanced;
+
+import javax.persistence.EntityManager;
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.eclipse.persistence.testing.models.jpa.advanced.*;
+import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;
+import org.eclipse.persistence.testing.framework.junit.JUnitTestCase;
+import org.eclipse.persistence.sessions.server.ServerSession;
+
+/**
+ * @author DaraniY
+ */
+public class CacheImplJUnitTest extends JUnitTestCase {
+
+
+    public CacheImplJUnitTest() {
+        super();
+    }
+
+    public CacheImplJUnitTest(String name) {
+        super(name);
+    }
+
+    public static void setUpClass() throws Exception {
+    }
+
+    public static void tearDownClass() throws Exception {
+    }
+
+    public void setUp() {
+
+        super.setUp();
+        clearCache();
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite();
+        suite.setName("CacheImplJUnitTest");
+
+        suite.addTest(new CacheImplJUnitTest("testContains"));
+        suite.addTest(new CacheImplJUnitTest("testEvictClassObject"));
+        suite.addTest(new CacheImplJUnitTest("testEvictClass"));
+        suite.addTest(new CacheImplJUnitTest("testEvictAll"));
+        return new TestSetup(suite) {
+
+            protected void setUp() {
+                ServerSession session = JUnitTestCase.getServerSession();
+
+                new AdvancedTableCreator().replaceTables(session);
+            }
+
+            protected void tearDown() {
+                clearCache();
+            }
+        };
+    }
+
+    /**
+     * Test of contains method, of class CacheImpl.
+     */
+    public void testContains() {
+        System.out.println("contains");
+        EntityManager em1 = createEntityManager("default1");
+        beginTransaction(em1);
+        Employee e1 = new Employee();
+        e1.setFirstName("ellie1");
+        e1.setId(101);
+        em1.persist(e1);
+        commitTransaction(em1);
+        closeEntityManager(em1);
+        try {
+            boolean result = getEntityManagerFactory("default1").getCache().contains(Employee.class, 101);
+            assertTrue("Assertion Error",result);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Test of evict method, of class CacheImpl.
+     */
+    public void testEvictClassObject() {
+        String beforeCache;
+        String afterCache;
+        System.out.println("evictclassObject");
+        EntityManager em2 = createEntityManager("default1");
+        beginTransaction(em2);
+        Employee e2 = new Employee();
+        e2.setFirstName("ellie");
+        e2.setId(121);
+        em2.persist(e2);
+        commitTransaction(em2);
+        closeEntityManager(em2);
+        EntityManager em3 = createEntityManager("default1");
+        EntityManager em4=createEntityManager("default1");
+        try {
+            Employee emp1 = (Employee) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")).getServerSession().getIdentityMapAccessor().getFromIdentityMap(e2);
+            emp1.setFirstName("foo");
+            beforeCache = em3.find(Employee.class, 121).getFirstName();
+            getEntityManagerFactory("default1").getCache().evict(Employee.class, 121);
+            Employee e3 = (Employee) em4.find(Employee.class, 121);
+            afterCache = e3.getFirstName();
+            assertNotSame("Assertion Error", beforeCache, afterCache);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeEntityManager(em3);
+            closeEntityManager(em4);
+        }
+    }
+
+    /**
+     * Test of evict method, of class CacheImpl.
+     */
+    public void testEvictClass() {
+        System.out.println("evict_class");
+        EntityManager em5 = createEntityManager("default1");
+        beginTransaction(em5);
+        Employee e4 = new Employee();
+        e4.setFirstName("ellie");
+        e4.setId(131);
+        em5.persist(e4);
+        commitTransaction(em5);
+        closeEntityManager(em5);
+        EntityManager em6 = createEntityManager("default1");
+        EntityManager em7 = createEntityManager("default1");
+        try {
+            Employee emp2 = (Employee) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")).getServerSession().getIdentityMapAccessor().getFromIdentityMap(e4);
+            emp2.setFirstName("food");
+            String expected = em6.find(Employee.class, 131).getFirstName();
+            getEntityManagerFactory("default1").getCache().evict(Employee.class);
+            Employee e5 = (Employee) em7.find(Employee.class, 131);
+            String actual = e5.getFirstName();
+            assertNotSame("Assertion Error", expected, actual);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeEntityManager(em6);
+            closeEntityManager(em7);
+        }
+    }
+
+    /**
+     * Test of evictAll method, of class CacheImpl.
+     */
+    public void testEvictAll() {
+        System.out.println("evictAll");
+        EntityManager em8 = createEntityManager("default1");
+        beginTransaction(em8);
+        Employee e6 = new Employee();
+        e6.setFirstName("ellie");
+        e6.setId(141);
+        Department d1 = new Department();
+        d1.setId(3);
+        d1.setName("Computers");
+        em8.persist(d1);
+        em8.persist(e6);
+        commitTransaction(em8);
+        String expectedEmp = e6.getFirstName();
+        String expectedDept = d1.getName();
+        closeEntityManager(em8);
+        EntityManager em9 = createEntityManager("default1");
+        try {
+            Employee emp3 = (Employee) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")).getServerSession().getIdentityMapAccessor().getFromIdentityMap(e6);
+            Department dept1 = (Department) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")).getServerSession().getIdentityMapAccessor().getFromIdentityMap(d1);
+            emp3.setFirstName("foo");
+            dept1.setName("science");
+            getEntityManagerFactory("default1").getCache().evictAll();
+            Employee e4 = (Employee) em9.find(Employee.class, 141);
+            String actualEmp = e4.getFirstName();
+            Department d2 = (Department) em9.find(Department.class, 3);
+            String actualDept = d2.getName();
+            assertEquals("Assertion Error", expectedEmp, actualEmp);
+            assertEquals("Assertion Error", expectedDept, actualDept);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeEntityManager(em9);
+        }
+
+    }
+}
\ No newline at end of file
Index: jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/FullRegressionTestSuite.java
===================================================================
--- jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/FullRegressionTestSuite.java	(revision 2820)
+++ jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/FullRegressionTestSuite.java	(working copy)
@@ -17,6 +17,7 @@
 
 import org.eclipse.persistence.testing.tests.jpa.advanced.AdvancedJPAJunitTest;
 import org.eclipse.persistence.testing.tests.jpa.advanced.AdvancedJunitTest;
+import org.eclipse.persistence.testing.tests.jpa.advanced.CacheImplJUnitTest;
 import org.eclipse.persistence.testing.tests.jpa.advanced.NamedNativeQueryJUnitTest;
 import org.eclipse.persistence.testing.tests.jpa.advanced.CallbackEventJUnitTestSuite;
 import org.eclipse.persistence.testing.tests.jpa.advanced.EntityManagerJUnitTestSuite;
@@ -76,6 +77,7 @@
         TestSuite suite = new TestSuite();
         suite.setName("advanced");
         suite.addTest(NamedNativeQueryJUnitTest.suite());
+        suite.addTest(CacheImplJUnitTest.suite());
         suite.addTest(CallbackEventJUnitTestSuite.suite());
         suite.addTest(EntityManagerJUnitTestSuite.suite());
         suite.addTest(SQLResultSetMappingTestSuite.suite());
Index: jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/CacheImpl.java
===================================================================
--- jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/CacheImpl.java	(revision 0)
+++ jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/CacheImpl.java	(revision 0)
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c)  2008, Sun Microsystems, Inc. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *     DaraniY  = 1.0 - Initialize contribution
+ ******************************************************************************/
+package org.eclipse.persistence.internal.jpa;
+
+import java.util.Vector;
+import javax.persistence.Cache;
+import org.eclipse.persistence.internal.sessions.AbstractSession;
+import org.eclipse.persistence.descriptors.ClassDescriptor;
+import org.eclipse.persistence.sessions.IdentityMapAccessor;
+import org.eclipse.persistence.sessions.server.ServerSession;
+
+/**
+ *@inheritDoc
+ *@author DaraniY
+ */
+public class CacheImpl implements Cache {
+
+    private IdentityMapAccessor imap;
+    private EntityManagerFactoryImpl emf;
+    private ServerSession serversession;
+
+    public CacheImpl(EntityManagerFactoryImpl emf, IdentityMapAccessor imap) {
+        this.imap = imap;
+        this.emf = emf;
+        this.serversession = emf.getServerSession();
+    }
+
+    public boolean contains(Class cls, Object primaryKey) {
+        return imap.containsObjectInIdentityMap(createPKVector(cls, primaryKey), cls);
+    }
+
+    public void evict(Class cls, Object primaryKey) {
+       imap.invalidateObject(createPKVector(cls, primaryKey), cls);
+    }
+
+    public void evict(Class cls) {
+        imap.invalidateClass(cls);
+    }
+
+    public void evictAll() {
+        imap.invalidateAll();
+    }
+
+    private Vector createPKVector(Class cls, Object primaryKey){
+        Class clss = cls;
+        Object pkey = primaryKey;
+        ClassDescriptor cdesc = serversession.getDescriptor(clss);
+        CMP3Policy cmp = (CMP3Policy) (cdesc.getCMPPolicy());
+        Vector pk = cmp.createPkVectorFromKey(pkey, (AbstractSession) serversession);
+        return pk;
+    }
+}
\ No newline at end of file
Index: jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerFactoryImpl.java
===================================================================
--- jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerFactoryImpl.java	(revision 2820)
+++ jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerFactoryImpl.java	(working copy)
@@ -14,6 +14,7 @@
 
 import java.util.Map;
 
+import javax.persistence.Cache;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.FlushModeType;
@@ -43,6 +44,8 @@
 * @since   TopLink 10.1.3 EJB 3.0 Preview
 */
 public class EntityManagerFactoryImpl implements EntityManagerFactory {
+    /** Reference to Cache Interface. */
+    private Cache myCache;
     /** Reference to the ServerSession for this deployment. */
     protected volatile ServerSession serverSession;
     /** EntityManagerSetupImpl that deployed this factory. */
@@ -326,4 +329,12 @@
     public void setShouldValidateExistence(boolean shouldValidateExistence) {
         this.shouldValidateExistence = shouldValidateExistence;
     }
+
+    public Cache getCache(){
+        ServerSession session=this.getServerSession();
+        if (myCache == null){
+            myCache = new CacheImpl(this,session.getIdentityMapAccessor());
+                }
+            return myCache;
+        }
 }
Index: jpa/plugins/javax.persistence/src/javax/persistence/Cache.java
===================================================================
--- jpa/plugins/javax.persistence/src/javax/persistence/Cache.java	(revision 0)
+++ jpa/plugins/javax.persistence/src/javax/persistence/Cache.java	(revision 0)
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c)  2008, Sun Microsystems, Inc. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *     DaraniY  = 1.0 - Initialize contribution
+ ******************************************************************************/
+package javax.persistence;
+
+/**
+ * Interface used to interact with the second-level cache.
+ * If a cache is not in use, the methods of this interface have
+ * no effect, except for contains, which returns false.
+ *
+ */
+public interface Cache {
+
+    /**
+     *  Whether the cache contains data for the given entity.
+     */
+    public boolean contains(Class cls, Object primaryKey);
+
+    /**
+     * Remove the data for the given entity from the cache.
+     */
+    public void evict(Class cls, Object primaryKey);
+
+    /**
+     * Remove the data for entities of the specified class (and its
+     * subclasses) from the cache.
+     */
+    public void evict(Class cls);
+
+    /**
+     * Clear the cache.
+     */
+    public void evictAll();
+}
Index: jpa/plugins/javax.persistence/src/javax/persistence/EntityManagerFactory.java
===================================================================
--- jpa/plugins/javax.persistence/src/javax/persistence/EntityManagerFactory.java	(revision 2820)
+++ jpa/plugins/javax.persistence/src/javax/persistence/EntityManagerFactory.java	(working copy)
@@ -82,4 +82,13 @@
     * until a call to close has been made.
     */
     public boolean isOpen();
+
+    /**
+     * Access the cache that is associated with the entity manager
+     * factory(the "second level cache").
+     * @return instance of the Cache interface
+     * @throws IllegalStateException if the entity manager factory
+     * has been closed.
+     */
+    public Cache getCache();
 }

Back to the top