Search in sources :

Example 1 with InstanceLifecycleEvent

use of javax.jdo.listener.InstanceLifecycleEvent in project tests by datanucleus.

the class JDOPersistenceTestCase method cleanup.

protected void cleanup(Set<Object> toCleanup) throws Exception {
    // Keep track of what is being deleted so we don't need to
    // delete what has already been deleted by the cascade.
    Set<Object> deleted = new HashSet<>();
    DeleteLifecycleListener listener = new DeleteLifecycleListener() {

        @Override
        public void preDelete(InstanceLifecycleEvent event) {
        // Nothing to do
        }

        @Override
        public void postDelete(InstanceLifecycleEvent event) {
            Object id = ((Persistable) event.getSource()).dnGetObjectId();
            deleted.add(id);
        }
    };
    try (PersistenceManager pm = pmf.getPersistenceManager()) {
        pm.addInstanceLifecycleListener(listener, Object.class);
        Set<Object> toDelete = toCleanup;
        Set<Object> retry = new HashSet<>();
        int attempts = 0;
        do {
            // Try to delete
            for (Object id : toDelete) {
                try {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Cleaning up: " + id);
                    }
                    if (deleted.contains(id)) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Already deleted: " + id);
                        }
                    } else {
                        Object object = pm.getObjectById(id, false);
                        boolean cleanerRun = false;
                        for (Entry<String, Consumer<PersistenceManager>> entry : cleanersByPrefix.entrySet()) {
                            if (object.getClass().getName().startsWith(entry.getKey())) {
                                entry.getValue().accept(pm);
                                cleanerRun = true;
                                break;
                            }
                        }
                        if (!cleanerRun) {
                            LOG.debug(">> calling deletePersistent on " + StringUtils.toJVMIDString(object) + " state=" + JDOHelper.getObjectState(object));
                            pm.deletePersistent(object);
                        }
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Cleaned " + id + " successfully");
                        }
                    }
                } catch (Exception e) {
                    LOG.warn("Exception in delete process", e);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Failed, retrying later: " + id);
                    }
                    retry.add(id);
                }
            }
            attempts++;
            if (!retry.isEmpty() && attempts > MAX_CLEANUP_ATTEMPTS) {
                // Give up
                retry.clear();
            // throw new Exception("Fail to cleanup the following object(s) after " + attempts + " attempts: " + retry);
            }
            // Try again
            toDelete.clear();
            toDelete.addAll(retry);
            retry.clear();
        } while (!toDelete.isEmpty());
    }
}
Also used : Persistable(org.datanucleus.enhancement.Persistable) PersistenceManager(javax.jdo.PersistenceManager) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) InstanceLifecycleEvent(javax.jdo.listener.InstanceLifecycleEvent) Consumer(java.util.function.Consumer) DeleteLifecycleListener(javax.jdo.listener.DeleteLifecycleListener) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with InstanceLifecycleEvent

use of javax.jdo.listener.InstanceLifecycleEvent in project tests by datanucleus.

the class AttachDetachTest method testAttachCleanCollectionWithNonPCElements.

/**
 * Test of attaching an object with a collection that has no changed elements and
 * where the elements of the collection are non-PC.
 */
public void testAttachCleanCollectionWithNonPCElements() {
    try {
        Object objectId = null;
        ClassWithNonPCCollection obj1 = new ClassWithNonPCCollection();
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            // test detach and attach
            tx.begin();
            obj1.getElements().add("elem1");
            obj1.getElements().add("elem2");
            pm.makePersistent(obj1);
            tx.commit();
            objectId = pm.getObjectId(obj1);
            // detach
            tx.begin();
            obj1 = (ClassWithNonPCCollection) pm.getObjectById(objectId, true);
            pm.getFetchPlan().addGroup("detach");
            obj1 = (ClassWithNonPCCollection) pm.detachCopy(obj1);
            tx.commit();
            // add a storeLifeCycleListener to check what gets stored
            final Collection<Object> storedElements = new ArrayList<>();
            pm.addInstanceLifecycleListener(new StoreLifecycleListener() {

                public void preStore(InstanceLifecycleEvent event) {
                }

                public void postStore(InstanceLifecycleEvent event) {
                    storedElements.add(event.getSource());
                }
            });
            // attach the unchanged Object
            tx.begin();
            obj1 = (ClassWithNonPCCollection) pm.makePersistent(obj1);
            tx.commit();
            // test that nothing has been stored
            assertTrue(storedElements.isEmpty());
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ClassWithNonPCCollection.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) StoreLifecycleListener(javax.jdo.listener.StoreLifecycleListener) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) ClassWithNonPCCollection(org.datanucleus.samples.detach.ClassWithNonPCCollection) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) InstanceLifecycleEvent(javax.jdo.listener.InstanceLifecycleEvent)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 InstanceLifecycleEvent (javax.jdo.listener.InstanceLifecycleEvent)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Consumer (java.util.function.Consumer)1 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserException (javax.jdo.JDOUserException)1 Transaction (javax.jdo.Transaction)1 DeleteLifecycleListener (javax.jdo.listener.DeleteLifecycleListener)1 StoreLifecycleListener (javax.jdo.listener.StoreLifecycleListener)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 Persistable (org.datanucleus.enhancement.Persistable)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 ClassWithNonPCCollection (org.datanucleus.samples.detach.ClassWithNonPCCollection)1