use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class AbstractTransactionController method lookupActiveUnitOfWork.
/**
* INTERNAL:
* Return the unit of work associated with the active external transaction.
* Return null if no transaction is active, or if no uow has been associated with
* the active transaction yet.
*/
public UnitOfWorkImpl lookupActiveUnitOfWork(Object transaction) {
if (transaction == null) {
return null;
}
Object transactionKey = getTransactionKey(transaction);
// PERF: Cache the active unit of work in a thread local.
// This is just a heuristic, so uses == and no tx-key and direct access as extremely high throughput.
UnitOfWorkImpl activeUnitOfWork = this.activeUnitOfWorkThreadLocal.get();
if (activeUnitOfWork != null) {
if (transaction == activeUnitOfWork.getTransaction()) {
return activeUnitOfWork;
}
}
activeUnitOfWork = getUnitsOfWork().get(transactionKey);
if (activeUnitOfWork != null) {
activeUnitOfWork.setTransaction(transaction);
}
this.activeUnitOfWorkThreadLocal.set(activeUnitOfWork);
return activeUnitOfWork;
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class ConcurrencyTest method testTransitionToDeferedFailure.
/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testTransitionToDeferedFailure() {
if (isOnServer()) {
return;
}
Integer toWaitOn = 4;
Thread thread1 = null;
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
ConcurrencyA a = new ConcurrencyA();
ConcurrencyA a2 = new ConcurrencyA();
ConcurrencyB b = new ConcurrencyB();
ConcurrencyC c = new ConcurrencyC();
em.persist(a);
em.persist(a2);
em.persist(c);
em.persist(b);
em.getTransaction().commit();
em.close();
try {
thread1 = new Thread(new TransitionRunner1(toWaitOn, b, c, emf));
thread1.start();
em = emf.createEntityManager();
a = em.find(ConcurrencyA.class, a.getId());
a2 = em.find(ConcurrencyA.class, a2.getId());
b = em.find(ConcurrencyB.class, b.getId());
c = em.find(ConcurrencyC.class, c.getId());
a2.setName(System.currentTimeMillis() + "_A");
a.setConcurrencyB(b);
a.setConcurrencyC(c);
UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
try {
Thread.sleep(20000);
synchronized (toWaitOn) {
toWaitOn.notifyAll();
toWaitOn.wait();
}
} catch (InterruptedException e) {
}
uow.issueSQLbeforeCompletion(true);
uow.mergeClonesAfterCompletion();
synchronized (toWaitOn) {
toWaitOn.notifyAll();
}
try {
thread1.join(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
if (thread1.isAlive()) {
thread1.interrupt();
}
em = emf.createEntityManager();
em.getTransaction().begin();
em.remove(em.find(ConcurrencyA.class, a.getId()));
em.remove(em.find(ConcurrencyA.class, a2.getId()));
em.remove(em.find(ConcurrencyC.class, c.getId()));
em.remove(em.find(ConcurrencyB.class, b.getId()));
em.getTransaction().commit();
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInFakeBirthState0.
public void testClearWhileEntityManagerInFakeBirthState0() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = null;
UnitOfWorkImpl uow = null;
Map cloneToOriginalsMap = null;
Department dept = null;
try {
em = emf.createEntityManager();
// get the underlying uow
uow = getUnitOfWorkFromEntityManager(em);
// force a get on the map to lazy initialize an empty map
cloneToOriginalsMap = uow.getCloneToOriginals();
// verify size 0
// we don't have access to the protected function uow.hasCloneToOriginals();
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size());
// Verify that cloneToOriginals is null and not lazy initialized
dept = new Department();
cloneToOriginalsMap.put(null, dept);
// verify size 1
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
// verify we are in birth state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
// simulate a clear() call in the middle of a merge
em.clear();
// verify that the uow ignored the clear call
int lifecycleAfter = uow.getLifecycle();
assertEquals("Unchanged Birth state 0 is not set ", 0, lifecycleAfter);
// verify that a map previously set on the em was not cleared to null by the clear
// verify size 0
cloneToOriginalsMap = uow.getCloneToOriginals();
assertNotNull("cloneToOriginals Map must not be null after a clear in Birth state", cloneToOriginalsMap);
assertEquals("cloneToOriginalsMap must be not be cleared to size 0", 1, cloneToOriginalsMap.size());
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInFakeMergePendingState4.
// This test is a pure unit test that directly sets and tries to clear the uow state
// There are no actual entities managed in this example
public void testClearWhileEntityManagerInFakeMergePendingState4() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = null;
UnitOfWorkImpl uow = null;
Map cloneToOriginalsMap = null;
Department dept = null;
try {
em = emf.createEntityManager();
// get the underlying uow
uow = getUnitOfWorkFromEntityManager(em);
// force a get on the map to lazy initialize an empty map
cloneToOriginalsMap = uow.getCloneToOriginals();
// verify size 0
// we don't have access to the protected function uow.hasCloneToOriginals();
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size());
// Verify that cloneToOriginals is null and not lazy initialized
dept = new Department();
cloneToOriginalsMap.put(null, dept);
// verify size 1
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
// verify we are in birth state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
// setup the uow in a simulated state
// set state to 4 = MergePending
uow.setPendingMerge();
// (via backdoor function) verify we are in PendingMerge state
int lifecycleInMerge = uow.getLifecycle();
assertEquals("MergePending state 4 is not set ", 4, lifecycleInMerge);
// simulate a clear() call in the middle of a merge
// 326097: This assertion used to be ignored by a catch block - it is a valid test but we want to clear on the EM not the UOW
em.clear();
// verify that the uow ignored the clear call
int lifecycleAfter = uow.getLifecycle();
assertEquals("UnModified MergePending state 4 should still be 4 and not Birth state 0 after a clear() ", 4, lifecycleAfter);
// verify that a map previously set on the uow was cleared to null by the
// verify size 0
assertNotNull("cloneToOriginals Map must not be null after a clear in *Pending state", cloneToOriginalsMap);
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit.
public void testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
Department dept = null;
try {
em.getTransaction().begin();
dept = new Department();
// A merge will not populate the @Id field and will result in a PK null exception in any find later
// A persist will populate the @Id field
em.persist(dept);
// simulate an attempt to call close() while we are in the middle of a commit
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em);
// get lifecycle state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
em.clear();
int lifecycleAfter = uow.getLifecycle();
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter);
em.getTransaction().commit();
// don't clear em - leave following line commented
// em.clear();
int lifecycleAfterCommit = uow.getLifecycle();
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
Aggregations