use of org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork in project eclipselink by eclipse-ee4j.
the class AdvancedJunitTest method testEL254937.
public void testEL254937() {
// Should not run in the server - bug 264589
if (!isOnServer()) {
EntityManager em = createEntityManager();
beginTransaction(em);
LargeProject lp1 = new LargeProject();
lp1.setName("one");
em.persist(lp1);
commitTransaction(em);
em = createEntityManager();
beginTransaction(em);
em.remove(em.find(LargeProject.class, lp1.getId()));
em.flush();
JpaEntityManager eclipselinkEm = (JpaEntityManager) em.getDelegate();
RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) eclipselinkEm.getActiveSession();
// duplicate the beforeCompletion call
uow.issueSQLbeforeCompletion();
// commit the transaction
uow.setShouldTerminateTransaction(true);
uow.commitTransaction();
// duplicate the AfterCompletion call. This should merge, removing the LargeProject from the shared cache
uow.mergeClonesAfterCompletion();
em = createEntityManager();
LargeProject cachedLargeProject = em.find(LargeProject.class, lp1.getId());
closeEntityManager(em);
assertTrue("Entity removed during flush was not removed from the shared cache on commit", cachedLargeProject == null);
}
}
use of org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.
// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
Employee employee = new Employee();
employee.setLastName("SomeName");
Address addr = new Address();
addr.setCity("Douglas");
try {
em.persist(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.setAddress(addr);
addr.getEmployees().add(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
clearCache();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.getAddress().setCountry("country");
employee.getAddress().getEmployees().size();
employee.setAddress((Address) null);
em.remove(employee);
commitTransaction(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
assertNull("Employee Not Deleted", employee);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
} finally {
try {
em = createEntityManager();
clearCache();
beginTransaction(em);
em.remove(em.find(Address.class, addr.getID()));
commitTransaction(em);
} catch (RuntimeException ex) {
// ignore
}
}
}
use of org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testChangeSetForNewObject.
// Bug 357103
public void testChangeSetForNewObject() {
EntityManager em = createEntityManager();
beginTransaction(em);
Customer cust = new Customer();
cust.setName("Joe");
em.persist(cust);
Order order = new Order();
order.setQuantity(1);
em.persist(order);
cust.addOrder(order);
commitTransaction(em);
RepeatableWriteUnitOfWork uow = null;
try {
beginTransaction(em);
cust = em.find(Customer.class, cust.getCustomerId());
Order order2 = new Order();
order2.setQuantity(2);
order2.setShippingAddress("123 Main St.");
em.persist(order2);
cust.addOrder(order2);
EntityManagerImpl impl = (EntityManagerImpl) JpaHelper.getEntityManager(em);
uow = impl.getActivePersistenceContext(null);
em.flush();
UnitOfWorkChangeSet uowChangeSet = uow.getCumulativeUOWChangeSet();
ObjectChangeSet customerChangeSet = uowChangeSet.getCloneToObjectChangeSet().get(cust);
CollectionChangeRecord orderChangeRecord = (CollectionChangeRecord) customerChangeSet.getChangesForAttributeNamed("orders");
Iterator<ObjectChangeSet> i = orderChangeRecord.getAddObjectList().keySet().iterator();
while (i.hasNext()) {
ObjectChangeSet orderChangeSet = i.next();
assertTrue("There are changes in the change set. There should be no changes for a new object.", orderChangeSet.getChanges().isEmpty());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rollbackTransaction(em);
beginTransaction(em);
cust = em.find(Customer.class, cust.getCustomerId());
em.remove(cust);
commitTransaction(em);
}
}
use of org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.
// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
Employee employee = new Employee();
employee.setLastName("SomeName");
Address addr = new Address();
addr.setCity("Douglas");
try {
em.persist(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.setAddress(addr);
addr.getEmployees().add(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager();
clearCache();
beginTransaction(em);
((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
employee = em.find(Employee.class, employee.getId());
employee.getAddress().setCountry("country");
employee.getAddress().getEmployees().size();
employee.setAddress((Address) null);
em.remove(employee);
commitTransaction(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
assertNull("Employee Not Deleted", employee);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
} finally {
try {
em = createEntityManager();
clearCache();
beginTransaction(em);
em.remove(em.find(Address.class, addr.getID()));
commitTransaction(em);
} catch (RuntimeException ex) {
// ignore
}
}
}
use of org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork in project eclipselink by eclipse-ee4j.
the class IsolatedCacheTestSuite method testCacheIsolationDBQueryHit.
public void testCacheIsolationDBQueryHit() throws Exception {
EntityManager em = createEntityManager("fieldaccess");
// Step 1 - get an isolated item in the cache.
beginTransaction(em);
try {
IsolatedItem item = new IsolatedItem();
item.setDescription("A phoney item");
item.setName("Phoney name");
em.persist(item);
commitTransaction(em);
// Step 2 - clear the entity manager and see if the item still exists
// in the uow cache.
beginTransaction(em);
em.clear();
RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) ((EntityManagerImpl) em.getDelegate()).getUnitOfWork();
assertFalse("The isolated item was not cleared from the shared cache", uow.getIdentityMapAccessor().containsObjectInIdentityMap(item));
assertFalse("The isolated item was not cleared from the uow cache", uow.getParent().getIdentityMapAccessor().containsObjectInIdentityMap(item));
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations