Search in sources :

Example 86 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-core by datanucleus.

the class Vector method initialise.

public void initialise(java.util.Vector newValue, Object oldValue) {
    if (newValue != null) {
        // Check for the case of serialised PC elements, and assign ObjectProviders to the elements without
        if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) {
            ExecutionContext ec = ownerOP.getExecutionContext();
            Iterator iter = newValue.iterator();
            while (iter.hasNext()) {
                Object pc = iter.next();
                ObjectProvider objSM = ec.findObjectProvider(pc);
                if (objSM == null) {
                    objSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
                }
            }
        }
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + newValue.size()));
        }
        // TODO This does clear+addAll : Improve this and work out which elements are added and which deleted
        if (backingStore != null) {
            if (SCOUtils.useQueuedUpdate(ownerOP)) {
                if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
                    ownerOP.getExecutionContext().addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore));
                    for (Object element : newValue) {
                        ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
                    }
                }
            } else {
                backingStore.clear(ownerOP);
                try {
                    backingStore.addAll(ownerOP, newValue, useCache ? 0 : -1);
                } catch (NucleusDataStoreException dse) {
                    NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
                }
            }
        }
        delegate.addAll(newValue);
        isCacheLoaded = true;
        makeDirty();
    }
}
Also used : CollectionClearOperation(org.datanucleus.flush.CollectionClearOperation) CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 87 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-core by datanucleus.

the class Collection method remove.

/**
 * Method to remove an element from the collection, and observe the flag for whether to allow cascade delete.
 * @param element The element
 * @param allowCascadeDelete Whether to allow cascade delete
 */
public boolean remove(Object element, boolean allowCascadeDelete) {
    makeDirty();
    if (useCache) {
        loadFromStore();
    }
    int size = useCache ? delegate.size() : -1;
    boolean contained = delegate.contains(element);
    boolean delegateSuccess = delegate.remove(element);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        ownerOP.getExecutionContext().getRelationshipManager(ownerOP).relationRemove(ownerMmd.getAbsoluteFieldNumber(), element);
    }
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            backingSuccess = contained;
            if (backingSuccess) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, backingStore, element, allowCascadeDelete));
            }
        } else {
            try {
                backingSuccess = backingStore.remove(ownerOP, element, size, allowCascadeDelete);
            } catch (NucleusDataStoreException dse) {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "remove", ownerMmd.getName(), dse));
                backingSuccess = false;
            }
        }
    }
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 88 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-core by datanucleus.

the class Collection method removeAll.

/**
 * Method to remove a Collection of elements.
 * @param elements The collection to remove
 * @return Whether they were removed successfully.
 */
public boolean removeAll(java.util.Collection elements) {
    if (elements == null) {
        throw new NullPointerException();
    } else if (elements.isEmpty()) {
        return true;
    }
    makeDirty();
    if (useCache) {
        loadFromStore();
    }
    int size = useCache ? delegate.size() : -1;
    java.util.Collection contained = null;
    if (backingStore != null && SCOUtils.useQueuedUpdate(ownerOP)) {
        // Check which are contained before updating the delegate
        contained = new java.util.HashSet();
        for (Object elem : elements) {
            if (contains(elem)) {
                contained.add(elem);
            }
        }
    }
    boolean delegateSuccess = delegate.removeAll(elements);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = elements.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (backingStore != null && ownerOP != null) {
        boolean backingSuccess = true;
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            if (contained != null && !contained.isEmpty()) {
                backingSuccess = false;
                for (Object element : contained) {
                    backingSuccess = true;
                    ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, backingStore, element, true));
                }
            }
        } else {
            try {
                backingSuccess = backingStore.removeAll(ownerOP, elements, size);
            } catch (NucleusDataStoreException dse) {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "removeAll", ownerMmd.getName(), dse));
                backingSuccess = false;
            }
        }
        if (!ownerOP.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
        return backingSuccess;
    }
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return delegateSuccess;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 89 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-core by datanucleus.

the class HashSet method remove.

/**
 * Method to remove an element from the collection, and observe the flag for whether to allow cascade delete.
 * @param element The element
 * @param allowCascadeDelete Whether to allow cascade delete
 */
public boolean remove(Object element, boolean allowCascadeDelete) {
    makeDirty();
    if (useCache) {
        loadFromStore();
    }
    int size = useCache ? delegate.size() : -1;
    boolean contained = delegate.contains(element);
    boolean delegateSuccess = delegate.remove(element);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        ownerOP.getExecutionContext().getRelationshipManager(ownerOP).relationRemove(ownerMmd.getAbsoluteFieldNumber(), element);
    }
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            backingSuccess = contained;
            if (backingSuccess) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, backingStore, element, allowCascadeDelete));
            }
        } else {
            try {
                backingSuccess = backingStore.remove(ownerOP, element, size, allowCascadeDelete);
            } catch (NucleusDataStoreException dse) {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "remove", ownerMmd.getName(), dse));
                backingSuccess = false;
            }
        }
    }
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 90 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-core by datanucleus.

the class HashSet method removeAll.

/**
 * Method to remove all elements from the collection from the HashSet.
 * @param elements The collection of elements to remove
 * @return Whether it was removed ok.
 */
public boolean removeAll(java.util.Collection elements) {
    if (elements == null) {
        throw new NullPointerException();
    } else if (elements.isEmpty()) {
        return true;
    }
    makeDirty();
    if (useCache) {
        loadFromStore();
    }
    int size = useCache ? delegate.size() : -1;
    Collection contained = null;
    if (backingStore != null && SCOUtils.useQueuedUpdate(ownerOP)) {
        // Check which are contained before updating the delegate
        contained = new java.util.HashSet();
        for (Object elem : elements) {
            if (contains(elem)) {
                contained.add(elem);
            }
        }
    }
    boolean delegateSuccess = delegate.removeAll(elements);
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = elements.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (backingStore != null && ownerOP != null) {
        boolean backingSuccess = true;
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            if (contained != null && !contained.isEmpty()) {
                backingSuccess = false;
                for (Object element : contained) {
                    backingSuccess = true;
                    ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, backingStore, element, true));
                }
            }
        } else {
            try {
                backingSuccess = backingStore.removeAll(ownerOP, elements, size);
            } catch (NucleusDataStoreException dse) {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "removeAll", ownerMmd.getName(), dse));
                backingSuccess = false;
            }
        }
        if (!ownerOP.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
        return backingSuccess;
    }
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return delegateSuccess;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) Collection(java.util.Collection) SCOCollection(org.datanucleus.store.types.SCOCollection) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)200 SQLException (java.sql.SQLException)98 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)74 ExecutionContext (org.datanucleus.ExecutionContext)73 PreparedStatement (java.sql.PreparedStatement)63 SQLController (org.datanucleus.store.rdbms.SQLController)60 ResultSet (java.sql.ResultSet)45 Iterator (java.util.Iterator)34 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 ObjectProvider (org.datanucleus.state.ObjectProvider)22 ArrayList (java.util.ArrayList)21 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21 Collection (java.util.Collection)20 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)19 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)18 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)14 List (java.util.List)13