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;
}
Aggregations