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()]));
}
}
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();
}
}
Aggregations