-
Notifications
You must be signed in to change notification settings - Fork 3
Flush and rollback
Flextrine provides two methods on the EntityManager to work with transactional write-behind - flush()
writes any pending changes to the database and rollback()
discards any changes since the last load/flush and returns the local repositories to their original state.
Flushing changes is performed by calling flush()
on the EntityManager:
em.flush();
This will perform any queued persists, removes and updates against the server - in other words this will synchronize the remote database to your local repositories.
As with most remote operations, flush()
returns an AsyncToken onto which you can attach result and fault responders.
On a successful call, flush()
returns an object with information about what operations were performed on which entities. This can be useful, for example, if you want to give your application the opportunity to react to changes once they have actually been written to the database. The following example fires off one of three signals depending on exactly what happened during the flush.
em.flush().addResponder(new AsyncResponder(onFlushResult, onFlushFault));
private function onFlushResult():void {
var changes:Object = resultEvent.result;
for each (var persistedEntity:Object in changes.persists)
entityPersistedSignal.dispatch(persistedEntity);
for each (var updatedEntity:Object in changes.updated)
entityUpdatedSignal.dispatch(updatedEntity);
for each (var removedEntity:Object in changes.removes)
entityRemovedSignal.dispatch(removedEntity);
}
private function faultEvent:FaultEvent, token:Object):void {
// Take some action on a fault
}
Note that the entities in changes.persists
and changes.updates
are the exact same instances that are in the EntityRepositories, and therefore will be the same instances that are in use within your application.
changes.removes
will contain an unmanaged, uninitialized entity. The only property of this that you can use will be its primary key.
Flextrine provides a rollback()
method which can be used to return all entities to their last loaded or flushed state. This can be used to discard any changes if you decide that you don't need them anymore. Typically you would use rollback()
if there is an error on flush()
. It can also potentially be used to implement Cancel buttons in dialog boxes (although detach
and merge
is probably a better solution for this use case).
// Make a change on a MANAGED entity
doc.name = "New name";
// Rollback all changes
em.rollback();
// This will display the original name of the doctor
trace(doc.name);
Note that em.rollback()
will rollback all changes - this includes:
- All persists
- All removes
- All updates to properties
- All updates to associations