|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 1998, 2008 Oracle. All rights reserved. |
| 3 |
* This program and the accompanying materials are made available under the |
| 4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| 5 |
* which accompanies this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Oracle - initial API and implementation from Oracle TopLink |
| 12 |
******************************************************************************/ |
| 13 |
package org.eclipse.persistence.testing.tests.jpa.advanced.concurrency; |
| 14 |
|
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
import javax.persistence.EntityManager; |
| 18 |
import javax.persistence.EntityManagerFactory; |
| 19 |
|
| 20 |
import junit.framework.AssertionFailedError; |
| 21 |
import junit.framework.Test; |
| 22 |
import junit.framework.TestSuite; |
| 23 |
|
| 24 |
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl; |
| 25 |
import org.eclipse.persistence.jpa.JpaEntityManager; |
| 26 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
| 27 |
import org.eclipse.persistence.testing.models.jpa.advanced.Department; |
| 28 |
import org.eclipse.persistence.testing.models.jpa.advanced.AdvancedTableCreator; |
| 29 |
|
| 30 |
/** |
| 31 |
* This test suite verifies that the state/lifecycle on a unitOfWork does not reset to 0 (Birth) |
| 32 |
* when a clearForClose() call is attempted in the middle of a *Pending state (1,2,4). |
| 33 |
* |
| 34 |
* Note: These tests verify internal API state that JPA functionality depends on. |
| 35 |
* The tests are tightly coupled to the implementation of the following functions. |
| 36 |
* Any change to the behavior of these functions may need to be reflected in these tests |
| 37 |
* |
| 38 |
* UnitOfWorkImpl.getCloneToOriginals() |
| 39 |
* UnitOfWorkImpl.setPendingMerge() |
| 40 |
* UnitOfWorkImpl.clearForClose() |
| 41 |
* |
| 42 |
* The server level JTA tests that verify that test this fix are the following |
| 43 |
* |
| 44 |
* 02/10/2009-1.1 Michael O'Brien |
| 45 |
* - 259993: Defer a clear() call to release() if uow lifecycle is 1,2 or 4 (*Pending). |
| 46 |
*/ |
| 47 |
public class LifecycleJUnitTest extends JUnitTestCase { |
| 48 |
|
| 49 |
public LifecycleJUnitTest() { |
| 50 |
super(); |
| 51 |
} |
| 52 |
|
| 53 |
public LifecycleJUnitTest(String name) { |
| 54 |
super(name); |
| 55 |
} |
| 56 |
|
| 57 |
public static Test suite() { |
| 58 |
TestSuite suite = new TestSuite("LifecycleJUnitTestSuite"); |
| 59 |
suite.addTest(new LifecycleJUnitTest("testSetup")); |
| 60 |
suite.addTest(new LifecycleJUnitTest("testClearWhileEntityManagerInFakeMergePendingState4")); |
| 61 |
suite.addTest(new LifecycleJUnitTest("testClearWhileEntityManagerInFakeBirthState0")); |
| 62 |
suite.addTest(new LifecycleJUnitTest("testClearWhileEntityManagerInCommitPendingStateWithClearAfterCommit")); |
| 63 |
suite.addTest(new LifecycleJUnitTest("testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit")); |
| 64 |
suite.addTest(new LifecycleJUnitTest("testClearAfterEntityManagerCommitFinished")); |
| 65 |
|
| 66 |
return suite; |
| 67 |
} |
| 68 |
|
| 69 |
// RESOURCE_LOCAL non container managed uow |
| 70 |
private UnitOfWorkImpl getUnitOfWorkFromEntityManager(EntityManager em) { |
| 71 |
return ((UnitOfWorkImpl)((JpaEntityManager)em).getActiveSession()).acquireUnitOfWork(); |
| 72 |
} |
| 73 |
|
| 74 |
public void testSetup() { |
| 75 |
clearCache(); |
| 76 |
new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession()); |
| 77 |
} |
| 78 |
|
| 79 |
public void finalize() { |
| 80 |
} |
| 81 |
|
| 82 |
// This test is a pure unit test that directly sets and tries to clear the uow state |
| 83 |
// There are no actual entities managed in this example |
| 84 |
public void testClearWhileEntityManagerInFakeMergePendingState4() { |
| 85 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 86 |
EntityManager em = null; |
| 87 |
UnitOfWorkImpl uow = null; |
| 88 |
Map cloneToOriginalsMap = null; |
| 89 |
Department dept = null; |
| 90 |
|
| 91 |
try { |
| 92 |
em = emf.createEntityManager(); |
| 93 |
// get the underlying uow |
| 94 |
uow = getUnitOfWorkFromEntityManager(em); |
| 95 |
|
| 96 |
// force a get on the map to lazy initialize an empty map |
| 97 |
cloneToOriginalsMap = uow.getCloneToOriginals(); |
| 98 |
// verify size 0 |
| 99 |
// we don't have access to the protected function uow.hasCloneToOriginals(); |
| 100 |
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size()); |
| 101 |
// Verify that cloneToOriginals is null and not lazy initialized |
| 102 |
dept = new Department(); |
| 103 |
cloneToOriginalsMap.put(null, dept); |
| 104 |
// verify size 1 |
| 105 |
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size()); |
| 106 |
|
| 107 |
// verify we are in birth state |
| 108 |
int lifecycleBefore = uow.getLifecycle(); |
| 109 |
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore); |
| 110 |
// setup the uow in a simulated state |
| 111 |
uow.setPendingMerge(); // set state to 4 = MergePending |
| 112 |
|
| 113 |
// (via backdoor function) verify we are in PendingMerge state |
| 114 |
int lifecycleInMerge = uow.getLifecycle(); |
| 115 |
assertEquals("MergePending state 4 is not set ", 4, lifecycleInMerge); |
| 116 |
// simulate a clear() call in the middle of a merge |
| 117 |
uow.clearForClose(false); |
| 118 |
|
| 119 |
// verify that the uow ignored the clear call |
| 120 |
int lifecycleAfter = uow.getLifecycle(); |
| 121 |
assertEquals("Unchanged MergePending state 4 is not set ", 4, lifecycleAfter); |
| 122 |
// verify that a map previously set on the uow was cleared to null by the |
| 123 |
// verify size 0 |
| 124 |
assertNotNull("cloneToOriginals Map must not be null after a clear in *Pending state", cloneToOriginalsMap); |
| 125 |
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size()); |
| 126 |
|
| 127 |
} catch (AssertionFailedError e) { |
| 128 |
e.printStackTrace(); |
| 129 |
} finally { |
| 130 |
closeEntityManager(em); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public void testClearWhileEntityManagerInFakeBirthState0() { |
| 135 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 136 |
EntityManager em = null; |
| 137 |
UnitOfWorkImpl uow = null; |
| 138 |
Map cloneToOriginalsMap = null; |
| 139 |
Department dept = null; |
| 140 |
try { |
| 141 |
em = emf.createEntityManager(); |
| 142 |
// get the underlying uow |
| 143 |
uow = getUnitOfWorkFromEntityManager(em); |
| 144 |
|
| 145 |
// force a get on the map to lazy initialize an empty map |
| 146 |
cloneToOriginalsMap = uow.getCloneToOriginals(); |
| 147 |
// verify size 0 |
| 148 |
// we don't have access to the protected function uow.hasCloneToOriginals(); |
| 149 |
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size()); |
| 150 |
// Verify that cloneToOriginals is null and not lazy initialized |
| 151 |
dept = new Department(); |
| 152 |
cloneToOriginalsMap.put(null, dept); |
| 153 |
// verify size 1 |
| 154 |
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size()); |
| 155 |
|
| 156 |
// verify we are in birth state |
| 157 |
int lifecycleBefore = uow.getLifecycle(); |
| 158 |
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore); |
| 159 |
|
| 160 |
// simulate a clear() call in the middle of a merge |
| 161 |
uow.clearForClose(false); |
| 162 |
|
| 163 |
// verify that the uow ignored the clear call |
| 164 |
int lifecycleAfter = uow.getLifecycle(); |
| 165 |
assertEquals("Unchanged Birth state 4 is not set ", 0, lifecycleAfter); |
| 166 |
// verify that a map previously set on the uow was cleared to null by the |
| 167 |
// verify size 0 |
| 168 |
cloneToOriginalsMap = uow.getCloneToOriginals(); |
| 169 |
assertNotNull("cloneToOriginals Map must not be null after a clear in Birth state", cloneToOriginalsMap); |
| 170 |
assertEquals("cloneToOriginalsMap must be size 1", 0, cloneToOriginalsMap.size()); |
| 171 |
|
| 172 |
} catch (AssertionFailedError e) { |
| 173 |
e.printStackTrace(); |
| 174 |
} finally { |
| 175 |
closeEntityManager(em); |
| 176 |
} |
| 177 |
} |
| 178 |
public void testClearWhileEntityManagerInFakeAfterExternalTransactionRolledBackState6() { |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* This test simulates EE container callbacks that could occur that affect em lifecycle state. |
| 184 |
* Specifically it tests whether we handle an attempt to clear an entityManager |
| 185 |
* that is in the middle of a commit. |
| 186 |
* We only clear the entityManager if we are in the states |
| 187 |
* (Birth == 0, WriteChangesFailed==3, Death==5 or AfterExternalTransactionRolledBack==6). |
| 188 |
* If we are in one of the following *Pending states we defer the clear() to the release() call later |
| 189 |
*/ |
| 190 |
public void testClearWhileEntityManagerInCommitPendingStateWithClearAfterCommit() { |
| 191 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 192 |
EntityManager em = emf.createEntityManager(); |
| 193 |
Department dept = null; |
| 194 |
//Equipment equip = null; |
| 195 |
try { |
| 196 |
em.getTransaction().begin(); |
| 197 |
dept = new Department(); |
| 198 |
// A merge will not populate the @Id field |
| 199 |
//em.merge(dept); |
| 200 |
// A persist will populate the @Id field |
| 201 |
em.persist(dept); |
| 202 |
|
| 203 |
// simulate an attempt to call close() while we are in the middle of a commit |
| 204 |
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em); |
| 205 |
|
| 206 |
// get lifecycle state |
| 207 |
int lifecycleBefore = uow.getLifecycle(); |
| 208 |
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore); |
| 209 |
|
| 210 |
uow.clearForClose(false); |
| 211 |
int lifecycleAfter = uow.getLifecycle(); |
| 212 |
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter); |
| 213 |
|
| 214 |
//em.flush(); |
| 215 |
em.getTransaction().commit(); |
| 216 |
|
| 217 |
// clear em |
| 218 |
uow.clearForClose(false); |
| 219 |
|
| 220 |
int lifecycleAfterCommit = uow.getLifecycle(); |
| 221 |
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit); |
| 222 |
} catch (RuntimeException ex){ |
| 223 |
if (isTransactionActive(em)){ |
| 224 |
rollbackTransaction(em); |
| 225 |
} |
| 226 |
closeEntityManager(em); |
| 227 |
throw ex; |
| 228 |
} finally { |
| 229 |
// return database to previous state |
| 230 |
//em = emf.createEntityManager(); |
| 231 |
em.getTransaction().begin(); |
| 232 |
// a find should goto the database |
| 233 |
// Execute query ReadObjectQuery(referenceClass=Department sql="SELECT ID, NAME FROM CMP3_DEPT WHERE (ID = ?)") |
| 234 |
// The remove operation has been performed on: org.eclipse.persistence.testing.models.jpa.advanced.Department@34ea34ea |
| 235 |
em.remove(em.find(Department.class, dept.getId())); |
| 236 |
// Execute query DeleteObjectQuery(org.eclipse.persistence.testing.models.jpa.advanced.Department@d5c0d5c) |
| 237 |
// Execute query DataModifyQuery(sql="DELETE FROM CMP3_DEPT_CMP3_EMPLOYEE WHERE (ADV_DEPT_ID = ?)") |
| 238 |
em.getTransaction().commit(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public void testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit() { |
| 243 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 244 |
EntityManager em = emf.createEntityManager(); |
| 245 |
Department dept = null; |
| 246 |
//Equipment equip = null; |
| 247 |
try { |
| 248 |
em.getTransaction().begin(); |
| 249 |
dept = new Department(); |
| 250 |
// A merge will not populate the @Id field and will result in a PK null exception in any find later |
| 251 |
//em.merge(dept); |
| 252 |
// A persist will populate the @Id field |
| 253 |
em.persist(dept); |
| 254 |
|
| 255 |
// simulate an attempt to call close() while we are in the middle of a commit |
| 256 |
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em); |
| 257 |
|
| 258 |
// get lifecycle state |
| 259 |
int lifecycleBefore = uow.getLifecycle(); |
| 260 |
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore); |
| 261 |
|
| 262 |
uow.clearForClose(false); |
| 263 |
int lifecycleAfter = uow.getLifecycle(); |
| 264 |
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter); |
| 265 |
|
| 266 |
//em.flush(); |
| 267 |
em.getTransaction().commit(); |
| 268 |
|
| 269 |
// don't clear em |
| 270 |
//uow.clearForClose(false); |
| 271 |
|
| 272 |
int lifecycleAfterCommit = uow.getLifecycle(); |
| 273 |
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit); |
| 274 |
} catch (RuntimeException ex){ |
| 275 |
if (isTransactionActive(em)){ |
| 276 |
rollbackTransaction(em); |
| 277 |
} |
| 278 |
closeEntityManager(em); |
| 279 |
throw ex; |
| 280 |
} finally { |
| 281 |
// return database to previous state |
| 282 |
//em = emf.createEntityManager(); |
| 283 |
em.getTransaction().begin(); |
| 284 |
// perform the following |
| 285 |
// Execute query ReadObjectQuery(referenceClass=Department sql="SELECT ID, NAME FROM CMP3_DEPT WHERE (ID = ?)") |
| 286 |
// The remove operation has been performed on: org.eclipse.persistence.testing.models.jpa.advanced.Department@34ea34ea |
| 287 |
em.remove(em.find(Department.class, dept.getId())); |
| 288 |
// Execute query DeleteObjectQuery(org.eclipse.persistence.testing.models.jpa.advanced.Department@d5c0d5c) |
| 289 |
// Execute query DataModifyQuery() |
| 290 |
em.getTransaction().commit(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// This clear should pass because the state is always 0 Begin except for inside the commit() |
| 295 |
public void testClearAfterEntityManagerCommitFinished() { |
| 296 |
EntityManagerFactory emf = getEntityManagerFactory(); |
| 297 |
EntityManager em = emf.createEntityManager(); |
| 298 |
Department dept = null; |
| 299 |
//Equipment equip = null; |
| 300 |
try { |
| 301 |
em.getTransaction().begin(); |
| 302 |
dept = new Department(); |
| 303 |
em.persist(dept); |
| 304 |
|
| 305 |
// simulate an attempt to call close() while we are in the middle of a commit |
| 306 |
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em); |
| 307 |
|
| 308 |
// get lifecycle state |
| 309 |
int lifecycleBefore = uow.getLifecycle(); |
| 310 |
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore); |
| 311 |
|
| 312 |
uow.clearForClose(false); |
| 313 |
int lifecycleAfter = uow.getLifecycle(); |
| 314 |
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter); |
| 315 |
|
| 316 |
em.getTransaction().commit(); |
| 317 |
|
| 318 |
uow.clearForClose(false); |
| 319 |
int lifecycleAfterCommit = uow.getLifecycle(); |
| 320 |
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit); |
| 321 |
} catch (RuntimeException ex){ |
| 322 |
if (isTransactionActive(em)){ |
| 323 |
rollbackTransaction(em); |
| 324 |
} |
| 325 |
closeEntityManager(em); |
| 326 |
throw ex; |
| 327 |
} finally { |
| 328 |
// return database to previous state |
| 329 |
em = emf.createEntityManager(); |
| 330 |
em.getTransaction().begin(); |
| 331 |
// If you get a PK null exception in any find then the entity was only merged and not persisted |
| 332 |
em.remove(em.find(Department.class, dept.getId())); |
| 333 |
//em.remove(em.find(Equipment.class, equip.getId())); |
| 334 |
em.getTransaction().commit(); |
| 335 |
} |
| 336 |
} |
| 337 |
} |