Search in sources :

Example 1 with CommitStateTransitionException

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

the class ExecutionContextImpl method postCommit.

/**
 * Commit any changes made to objects managed by the object manager to the database.
 */
public void postCommit() {
    if (properties.getFrequentProperties().getDetachAllOnCommit()) {
        // Detach-all-on-commit
        performDetachAllOnTxnEnd();
    }
    List failures = null;
    try {
        // Commit all enlisted ObjectProviders
        ApiAdapter api = getApiAdapter();
        ObjectProvider[] ops = enlistedOPCache.values().toArray(new ObjectProvider[enlistedOPCache.size()]);
        for (int i = 0; i < ops.length; ++i) {
            try {
                // TODO this if is due to sms that can have lc == null, why?, should not be here then
                if (ops[i] != null && ops[i].getObject() != null && (api.isPersistent(ops[i].getObject()) || api.isTransactional(ops[i].getObject()))) {
                    ops[i].postCommit(getTransaction());
                    // TODO Change this check so that we remove all objects that are no longer suitable for caching
                    if (properties.getFrequentProperties().getDetachAllOnCommit() && api.isDetachable(ops[i].getObject())) {
                        // "DetachAllOnCommit" - Remove the object from the L1 cache since it is now detached
                        removeObjectProviderFromCache(ops[i]);
                    }
                }
            } catch (RuntimeException e) {
                if (failures == null) {
                    failures = new ArrayList();
                }
                failures.add(e);
            }
        }
    } finally {
        resetTransactionalVariables();
    }
    if (failures != null && !failures.isEmpty()) {
        throw new CommitStateTransitionException((Exception[]) failures.toArray(new Exception[failures.size()]));
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) CommitStateTransitionException(org.datanucleus.exceptions.CommitStateTransitionException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ObjectProvider(org.datanucleus.state.ObjectProvider) ClassNotDetachableException(org.datanucleus.exceptions.ClassNotDetachableException) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException) RollbackStateTransitionException(org.datanucleus.exceptions.RollbackStateTransitionException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusFatalUserException(org.datanucleus.exceptions.NucleusFatalUserException) ClassNotPersistableException(org.datanucleus.exceptions.ClassNotPersistableException) NoPersistenceInformationException(org.datanucleus.exceptions.NoPersistenceInformationException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException) CommitStateTransitionException(org.datanucleus.exceptions.CommitStateTransitionException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) ObjectDetachedException(org.datanucleus.exceptions.ObjectDetachedException)

Example 2 with CommitStateTransitionException

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

the class ExecutionContextImpl method processNontransactionalAtomicChanges.

/**
 * Handler for all outstanding changes to be "committed" atomically.
 * If a transaction is active, non-tx writes are disabled, or atomic updates not enabled then will do nothing.
 * Otherwise will flush any updates that are outstanding (updates to an object), will perform detachAllOnCommit
 * if enabled (so user always has detached objects), update objects in any L2 cache, and migrates any
 * objects through lifecycle changes.
 * Is similar in content to "flush"+"preCommit"+"postCommit"
 * Note that this handling for updates is not part of standard JDO which expects non-tx updates to migrate an
 * object to P_NONTRANS_DIRTY rather than committing it directly.
 * TODO If any update fails we should throw the appropriate exception for the API
 */
protected void processNontransactionalAtomicChanges() {
    if (tx.isActive() || !tx.getNontransactionalWrite() || !tx.getNontransactionalWriteAutoCommit()) {
        return;
    }
    if (!dirtyOPs.isEmpty()) {
        // Make sure all non-tx dirty objects are enlisted so they get lifecycle changes
        for (ObjectProvider op : dirtyOPs) {
            if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
                NucleusLogger.TRANSACTION.debug(Localiser.msg("015017", StringUtils.toJVMIDString(op.getObject()), op.getInternalObjectId().toString()));
            }
            enlistedOPCache.put(op.getInternalObjectId(), op);
        }
        // Flush any outstanding changes to the datastore
        flushInternal(true);
        if (l2CacheEnabled) {
            // L2 caching of enlisted objects
            performLevel2CacheUpdateAtCommit();
        }
        if (properties.getFrequentProperties().getDetachAllOnCommit()) {
            // "detach-on-commit"
            performDetachAllOnTxnEndPreparation();
            performDetachAllOnTxnEnd();
        }
        // Make sure lifecycle changes take place to all "enlisted" objects
        List failures = null;
        try {
            // "commit" all enlisted ObjectProviders
            ApiAdapter api = getApiAdapter();
            ObjectProvider[] ops = enlistedOPCache.values().toArray(new ObjectProvider[enlistedOPCache.size()]);
            for (int i = 0; i < ops.length; ++i) {
                try {
                    // Run through "postCommit" to migrate the lifecycle state
                    if (ops[i] != null && ops[i].getObject() != null && api.isPersistent(ops[i].getObject()) && api.isDirty(ops[i].getObject())) {
                        ops[i].postCommit(getTransaction());
                    } else {
                        NucleusLogger.PERSISTENCE.debug(">> Atomic nontransactional processing : Not performing postCommit on " + ops[i]);
                    }
                } catch (RuntimeException e) {
                    if (failures == null) {
                        failures = new ArrayList();
                    }
                    failures.add(e);
                }
            }
        } finally {
            resetTransactionalVariables();
        }
        if (failures != null && !failures.isEmpty()) {
            throw new CommitStateTransitionException((Exception[]) failures.toArray(new Exception[failures.size()]));
        }
    }
    if (nontxProcessedOPs != null && !nontxProcessedOPs.isEmpty()) {
        for (ObjectProvider op : nontxProcessedOPs) {
            if (op != null && op.getLifecycleState() != null && op.getLifecycleState().isDeleted()) {
                removeObjectFromLevel1Cache(op.getInternalObjectId());
                removeObjectFromLevel2Cache(op.getInternalObjectId());
            }
        }
        nontxProcessedOPs.clear();
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) CommitStateTransitionException(org.datanucleus.exceptions.CommitStateTransitionException) ArrayList(java.util.ArrayList) ObjectProvider(org.datanucleus.state.ObjectProvider) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ClassNotDetachableException(org.datanucleus.exceptions.ClassNotDetachableException) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException) RollbackStateTransitionException(org.datanucleus.exceptions.RollbackStateTransitionException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusFatalUserException(org.datanucleus.exceptions.NucleusFatalUserException) ClassNotPersistableException(org.datanucleus.exceptions.ClassNotPersistableException) NoPersistenceInformationException(org.datanucleus.exceptions.NoPersistenceInformationException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException) CommitStateTransitionException(org.datanucleus.exceptions.CommitStateTransitionException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) ObjectDetachedException(org.datanucleus.exceptions.ObjectDetachedException)

Aggregations

ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ApiAdapter (org.datanucleus.api.ApiAdapter)2 ClassNotDetachableException (org.datanucleus.exceptions.ClassNotDetachableException)2 ClassNotPersistableException (org.datanucleus.exceptions.ClassNotPersistableException)2 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)2 CommitStateTransitionException (org.datanucleus.exceptions.CommitStateTransitionException)2 NoPersistenceInformationException (org.datanucleus.exceptions.NoPersistenceInformationException)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 NucleusFatalUserException (org.datanucleus.exceptions.NucleusFatalUserException)2 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)2 NucleusOptimisticException (org.datanucleus.exceptions.NucleusOptimisticException)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ObjectDetachedException (org.datanucleus.exceptions.ObjectDetachedException)2 RollbackStateTransitionException (org.datanucleus.exceptions.RollbackStateTransitionException)2 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)2 TransactionNotActiveException (org.datanucleus.exceptions.TransactionNotActiveException)2 ObjectProvider (org.datanucleus.state.ObjectProvider)2