|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2019 IBM Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + ******************************************************************************/ |
| 12 | + |
| 13 | +// Contributors: |
| 14 | +// 07/02/2019-3.0 Alexandre Jacob |
| 15 | +// - 544202: Fixed refresh of foreign key values when cacheKey was invalidated |
| 16 | + |
| 17 | +package org.eclipse.persistence.jpa.test.sharedcache; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | +import javax.persistence.EntityManager; |
| 22 | +import javax.persistence.EntityManagerFactory; |
| 23 | +import javax.persistence.EntityTransaction; |
| 24 | + |
| 25 | +import org.eclipse.persistence.jpa.JpaEntityManager; |
| 26 | +import org.eclipse.persistence.jpa.test.framework.DDLGen; |
| 27 | +import org.eclipse.persistence.jpa.test.framework.Emf; |
| 28 | +import org.eclipse.persistence.jpa.test.framework.EmfRunner; |
| 29 | +import org.eclipse.persistence.jpa.test.framework.Property; |
| 30 | +import org.eclipse.persistence.jpa.test.sharedcache.model.Student; |
| 31 | +import org.eclipse.persistence.jpa.test.sharedcache.model.Teacher; |
| 32 | +import org.eclipse.persistence.sessions.Session; |
| 33 | +import org.eclipse.persistence.sessions.SessionProfiler; |
| 34 | +import org.eclipse.persistence.tools.profiler.PerformanceMonitor; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | + |
| 39 | +@RunWith(EmfRunner.class) |
| 40 | +public class TestSharedCacheWithNonCacheable { |
| 41 | + |
| 42 | + @Emf(createTables = DDLGen.DROP_CREATE, classes = { Teacher.class, Student.class }, |
| 43 | + properties = { |
| 44 | + @Property(name = "eclipselink.logging.level", value = "FINE"), |
| 45 | + @Property(name = "eclipselink.logging.parameters", value = "true"), |
| 46 | + @Property(name = "eclipselink.cache.shared.default", value = "false"), |
| 47 | + @Property(name = "eclipselink.profiler", value = "PerformanceMonitor") |
| 48 | + }) |
| 49 | + private EntityManagerFactory emf; |
| 50 | + |
| 51 | + private Session session; |
| 52 | + |
| 53 | + private PerformanceMonitor performanceMonitor; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() { |
| 57 | + session = ((JpaEntityManager) emf.createEntityManager()).getServerSession(); |
| 58 | + performanceMonitor = (PerformanceMonitor) session.getProfiler(); |
| 59 | + |
| 60 | + // We populate DB with 2 students and 1 teacher |
| 61 | + Student student1 = new Student(1, "Picasso"); |
| 62 | + Student student2 = new Student(2, "Pablo"); |
| 63 | + |
| 64 | + Teacher teacher = new Teacher(1, "Foo", student1); |
| 65 | + |
| 66 | + final EntityManager em = this.emf.createEntityManager(); |
| 67 | + final EntityTransaction transaction = em.getTransaction(); |
| 68 | + |
| 69 | + transaction.begin(); |
| 70 | + em.persist(student1); |
| 71 | + em.persist(student2); |
| 72 | + em.persist(teacher); |
| 73 | + transaction.commit(); |
| 74 | + |
| 75 | + em.close(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testSharedCacheWithNonCacheable() throws Exception { |
| 80 | + // 1 : We want to get our Teacher 1. He is supposed to be in the shared cache. |
| 81 | + // The Student linked to our Teacher should not be in the cache. (@Noncacheable) |
| 82 | + { |
| 83 | + Teacher teacher = this.findTeacher(1); |
| 84 | + |
| 85 | + assertEquals(teacher.getId(), 1); |
| 86 | + assertEquals(teacher.getName(), "Foo"); |
| 87 | + |
| 88 | + // Our Teacher IS in shared cache |
| 89 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 1L); // + 1 |
| 90 | + |
| 91 | + Student student = teacher.getStudent(); |
| 92 | + |
| 93 | + assertEquals(student.getId(), 1); // trigger lazy loading of our Student |
| 94 | + |
| 95 | + // Our Student is NOT in shared cache |
| 96 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 1L); // no change |
| 97 | + } |
| 98 | + |
| 99 | + // 2 : We change our Teacher 1 in native SQL. The name and the linked student are changed. |
| 100 | + { |
| 101 | + EntityManager em = emf.createEntityManager(); |
| 102 | + |
| 103 | + EntityTransaction transaction = em.getTransaction(); |
| 104 | + |
| 105 | + transaction.begin(); |
| 106 | + em.createNativeQuery("update TEACHER set NAME = ?, STUDENT_ID = ? where ID = ?") |
| 107 | + .setParameter(1, "Bar") |
| 108 | + .setParameter(2, 2) |
| 109 | + .setParameter(3, 1) |
| 110 | + .executeUpdate(); |
| 111 | + transaction.commit(); |
| 112 | + |
| 113 | + em.close(); |
| 114 | + } |
| 115 | + |
| 116 | + // 3 : We want to get our Teacher 1 ONE MORE TIME. He is still supposed to be in the shared cache. |
| 117 | + // The Student linked to our Teacher should not be in the cache. (@Noncacheable) |
| 118 | + { |
| 119 | + Teacher teacher = this.findTeacher(1); |
| 120 | + |
| 121 | + assertEquals(teacher.getId(), 1); |
| 122 | + assertEquals(teacher.getName(), "Foo"); |
| 123 | + |
| 124 | + // Our Teacher IS in shared cache |
| 125 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 2L); // + 1 |
| 126 | + |
| 127 | + Student student = teacher.getStudent(); |
| 128 | + |
| 129 | + assertEquals(student.getId(), 1); // trigger lazy loading of our Student |
| 130 | + |
| 131 | + // Our Student is NOT in shared cache |
| 132 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 2L); // no change |
| 133 | + } |
| 134 | + |
| 135 | + // 4 : Now we clear shared cache. |
| 136 | + { |
| 137 | + emf.getCache().evict(Teacher.class); |
| 138 | + } |
| 139 | + |
| 140 | + // 5 : We want to get our Teacher 1 for the THIRD TIME. He is not in the shared cache anymore (invalidated) |
| 141 | + // Data should reflect our update from (2) |
| 142 | + { |
| 143 | + Teacher teacher = this.findTeacher(1); |
| 144 | + |
| 145 | + assertEquals(teacher.getId(), 1); |
| 146 | + assertEquals(teacher.getName(), "Bar"); // Updated |
| 147 | + |
| 148 | + // Our Teacher is NOT in shared cache |
| 149 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 2L); // no change |
| 150 | + |
| 151 | + Student student = teacher.getStudent(); |
| 152 | + |
| 153 | + assertEquals(student.getId(), 2); // trigger lazy loading of our Student |
| 154 | + |
| 155 | + // Our Student is NOT in shared cache |
| 156 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 2L); // no change |
| 157 | + } |
| 158 | + |
| 159 | + // 6 : We want to get our Teacher 1 for the FOURTH TIME. He is back in the shared. |
| 160 | + // Data should reflect our update from (2) |
| 161 | + { |
| 162 | + Teacher teacher = this.findTeacher(1); |
| 163 | + |
| 164 | + assertEquals(teacher.getId(), 1); |
| 165 | + assertEquals(teacher.getName(), "Bar"); // Updated |
| 166 | + |
| 167 | + // Our Teacher IS in shared cache |
| 168 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 3L); // + 1 |
| 169 | + |
| 170 | + Student student = teacher.getStudent(); |
| 171 | + |
| 172 | + // Before correction of bug 544202 this value was 1 because CacheKey.protectedForeignKeys was never updated |
| 173 | + assertEquals(student.getId(), 2); // trigger lazy loading of our Student |
| 174 | + |
| 175 | + // Our Student is NOT in shared cache |
| 176 | + assertEquals(performanceMonitor.getOperationTime(SessionProfiler.CacheHits), 3L); // no change |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private Teacher findTeacher(int id) { |
| 181 | + final EntityManager em = this.emf.createEntityManager(); |
| 182 | + Teacher teacher = em.find(Teacher.class, 1); |
| 183 | + teacher.getStudent().getId(); |
| 184 | + em.close(); |
| 185 | + |
| 186 | + return teacher; |
| 187 | + } |
| 188 | +} |
0 commit comments