// now write all the data to the datasources
if(writeToDataSource){
for (WritableDataSource source : dataSourcesAffected)
source.startTransaction();
for (NewObject newObject : newObjects) {
WritableDataSource source = ((WritableDataSource) DataSourceManager.getSourceByPrototype((Persistable)newObject.object.getPrototype()));
addAffectedSource(source);
NewObjectPersister persister = source.recordNewObject(newObject.object);
newObject.object.getId().persistIfNeeded(persister);
if(newObject.object instanceof PersistableObject)
((PersistableObject)newObject.object).lastModified = Transaction.currentTransaction().getTransactionTime();
else
((PersistableArray)newObject.object).lastModified = Transaction.currentTransaction().getTransactionTime();
}
for (Entry<TransactionValue,ChangeUpdate> changeEntry : changes.entrySet()) {
PersistableObject.commitPut(changeEntry,this);
}
for (WritableDataSource source : dataSourcesAffected)
source.commitTransaction();
}
// now make all the changes committed in the persisted objects so will
// be available to other threads
// TODO: Needs to be timestamp based in order be atomic
synchronized(commitsInProcess){
for (Entry<TransactionValue,ChangeUpdate> changeEntry : changes.entrySet()) {
TransactionValue transValue = changeEntry.getKey();
ChangeUpdate change = changeEntry.getValue();
Persistable target = change.target;
Object sourceValue = transValue.values.get(this);
if (target instanceof PersistableObject) {
((PersistableObject) target).lastModified = getTransactionTime();
if (transValue.commit(this)) {
if (change.key != null){
if(sourceValue instanceof Persistable && !(((Persistable)sourceValue).getId() instanceof NewObjectId))
sourceValue = ((Persistable)sourceValue).getId();
((PersistableObject) target).noCheckSet((String) change.key, sourceValue);
}
}
}
else if (target instanceof PersistableArray) {
((PersistableArray) target).lastModified = getTransactionTime();
if (change.key == null) {
transValue.values.remove(this);
// update the core Array with the transactional Array
int i = 0;
for (Object value : (PersistableArray) sourceValue){
if(value instanceof Persistable && !(((Persistable)value).getId() instanceof NewObjectId))
value = ((Persistable)value).getId();
((PersistableArray) target).noCheckSet(i++,value);
}
((PersistableArray) target).superPutLength(i);
}
//TODO: When a new value is added an array, we want to increment the other waiting transaction values indexes
}
}
commitsInProcess.remove(this);
}
}
catch (Exception e) {
// it failed, rollback
for (TransactionValue value : changes.keySet()) {
value.abort(this);
}
for (WritableDataSource source : dataSourcesAffected)
try {
source.abortTransaction();
} catch (Exception e1) {
e1.printStackTrace();
}
observedCallSet.clear();