* DB set <em>true</em>, else set <em>false</em> to improve performance.
*/
public void writeObjects(boolean needsReusePrepare) throws TransactionAbortedException, LockNotGrantedException
{
PersistenceBroker broker = transaction.getBroker();
ConnectionManagerIF connMan = broker.serviceConnectionManager();
boolean saveBatchMode = connMan.isBatchMode();
try
{
if (log.isDebugEnabled())
{
log.debug(
"PB is in internal tx: "
+ broker.isInTransaction()
+ " broker was: "
+ broker);
}
// all neccessary db operations are executed within a PersistenceBroker transaction:
if (!broker.isInTransaction())
{
log.error("PB associated with current odmg-tx is not in tx");
throw new TransactionAbortedException("Underlying PB is not in tx, was begin call done before commit?");
}
// Committing has to be done in two phases. First implicitly upgrade to lock on all related
// objects of objects in this transaction. Then the list of locked objects has to be
// reordered to solve referential integrity dependencies, then the objects are
// written into the database.
// 0. turn on the batch mode
connMan.setBatchMode(true);
// 1. mark objects no longer available in collection
// for delete and add new found objects
checkAllEnvelopes(broker);
// 2. mark all dependend objects for cascading insert/delete
cascadingDependents();
// 3. upgrade implicit locks.
//upgradeImplicitLocksAndCheckIfCommitIsNeeded();
upgradeLockIfNeeded();
// 4. Reorder objects
reorder();
// System.out.println("## ordering: ");
// for(int i = 0; i < mvOrderOfIds.size(); i++)
// {
// System.out.println("" + mvOrderOfIds.get(i));
// }
// System.out.println("## ordering end");
// 5. write objects.
writeAllEnvelopes(needsReusePrepare);
// 6. execute batch
connMan.executeBatch();
// 7. Update all Envelopes to new CleanState
cleanupEnvelopes(needsReusePrepare);
// 6. commit cleanup
afterWriteCleanup();
}
catch (Exception e)
{
connMan.clearBatch();
if(e instanceof OptimisticLockException)
{
log.warn("Optimistic lock exception while write objects", e);
// PB OptimisticLockException should be clearly signalled to the user
Object sourceObject = ((OptimisticLockException) e).getSourceObject();
throw new LockNotGrantedException("Optimistic lock exception occur, source object was (" + sourceObject + ")," +
" message was (" + e.getMessage() + ")");
}
else if(!(e instanceof RuntimeException))
{
log.warn("Error while write objects for tx " + transaction, e);
throw new ODMGRuntimeException("Unexpected error while write objects: " + e.getMessage());
}
else
{
log.warn("Error while write objects for tx " + transaction, e);
throw (RuntimeException) e;
}
}
finally
{
needsCommit = false;
connMan.setBatchMode(saveBatchMode);
}
}