Search in sources :

Example 1 with NucleusFatalUserException

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

the class ExecutionContextImpl method persistObjects.

/**
 * Method to persist an array of objects to the datastore.
 * @param objs The objects to persist
 * @return The persisted objects
 * @throws NucleusUserException Thrown if an error occurs during the persist process.
 *     Any exception could have several nested exceptions for each failed object persist
 */
public Object[] persistObjects(Object... objs) {
    if (objs == null) {
        return null;
    }
    Object[] persistedObjs = new Object[objs.length];
    // Allocate thread-local persistence info
    ThreadContextInfo threadInfo = acquireThreadContextInfo();
    try {
        if (threadInfo.attachedOwnerByObject == null) {
            threadInfo.attachedOwnerByObject = new HashMap();
        }
        if (threadInfo.attachedPCById == null) {
            threadInfo.attachedPCById = new HashMap();
        }
        if (!tx.isActive()) {
            threadInfo.nontxPersistDelete = true;
        }
        try {
            getStoreManager().getPersistenceHandler().batchStart(this, PersistenceBatchType.PERSIST);
            List<RuntimeException> failures = null;
            for (int i = 0; i < objs.length; i++) {
                try {
                    if (objs[i] != null) {
                        persistedObjs[i] = persistObjectWork(objs[i]);
                    }
                } catch (RuntimeException e) {
                    if (failures == null) {
                        failures = new ArrayList();
                    }
                    failures.add(e);
                }
            }
            if (failures != null && !failures.isEmpty()) {
                RuntimeException e = failures.get(0);
                if (e instanceof NucleusException && ((NucleusException) e).isFatal()) {
                    // Should really check all and see if any are fatal not just first one
                    throw new NucleusFatalUserException(Localiser.msg("010039"), failures.toArray(new Exception[failures.size()]));
                }
                throw new NucleusUserException(Localiser.msg("010039"), failures.toArray(new Exception[failures.size()]));
            }
        } finally {
            getStoreManager().getPersistenceHandler().batchEnd(this, PersistenceBatchType.PERSIST);
            if (!tx.isActive()) {
                // Commit any non-tx changes
                threadInfo.nontxPersistDelete = false;
                processNontransactionalAtomicChanges();
            }
        }
    } finally {
        // Deallocate thread-local persistence info
        releaseThreadContextInfo();
    }
    return persistedObjs;
}
Also used : ConcurrentReferenceHashMap(org.datanucleus.util.ConcurrentReferenceHashMap) HashMap(java.util.HashMap) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusFatalUserException(org.datanucleus.exceptions.NucleusFatalUserException) 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)1 HashMap (java.util.HashMap)1 ClassNotDetachableException (org.datanucleus.exceptions.ClassNotDetachableException)1 ClassNotPersistableException (org.datanucleus.exceptions.ClassNotPersistableException)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 CommitStateTransitionException (org.datanucleus.exceptions.CommitStateTransitionException)1 NoPersistenceInformationException (org.datanucleus.exceptions.NoPersistenceInformationException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusFatalUserException (org.datanucleus.exceptions.NucleusFatalUserException)1 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 NucleusOptimisticException (org.datanucleus.exceptions.NucleusOptimisticException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ObjectDetachedException (org.datanucleus.exceptions.ObjectDetachedException)1 RollbackStateTransitionException (org.datanucleus.exceptions.RollbackStateTransitionException)1 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)1 TransactionNotActiveException (org.datanucleus.exceptions.TransactionNotActiveException)1 ConcurrentReferenceHashMap (org.datanucleus.util.ConcurrentReferenceHashMap)1