How to gracefully cancel a transaction? [message #1064264] |
Tue, 18 June 2013 13:43 |
|
Hi all,
My editor uses a popup dialog to gather some user input and this dialog creates some new objects that are attached to the existing model, so it runs within an EMF transaction. The user has the option of cancelling the dialog, so it needs to be able to restore the model to its previous state. I'm assuming the best way to do this is to roll back the transaction (or abort? cancel?)
I've tried throwing an OperationCanceledException but this results in an INFO log message to be written to the error log. Is there any way to prevent these messages from being emitted and cluttering up the log?
Thanks,
Bob
|
|
|
|
|
|
Re: How to gracefully cancel a transaction? [message #1074352 is a reply to message #1064603] |
Fri, 26 July 2013 15:00 |
|
The solution (at least for my use-case) is to have the popup dialog manage the transaction. I have a Custom Feature that, when executed, displays a popup properties dialog for the selected Shape or Connection. When the dialog is created, it also creates a new transaction, like so:
private void initializeTransaction() {
try {
final InternalTransactionalEditingDomain transactionalDomain = (InternalTransactionalEditingDomain) editor
.getEditingDomain();
transaction = transactionalDomain.startTransaction(false, null);
getShell().addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
if (transaction.isActive()) {
if (cancel) {
transaction.rollback();
}
else {
try {
transaction.commit();
}
catch (RollbackException e) {
ErrorDialog.openError(getShell(), "Error Commiting Model Changes",
"An error occurred while trying to commit changes.", new Status(IStatus.ERROR,
Activator.PLUGIN_ID, e.getMessage(), e));
}
}
}
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
the boolean class variable "cancel" is set "true" in the dialog's okPressed() and "false" in cancelPressed() overrides and the dispose listener handles the closing of the transaction.
The Custom Feature also needs to override AbstractFeature.hasDoneChanges() which is returned by the popup dialog's hasDoneChanges(), like so:
public boolean hasDoneChanges() {
return transaction==null || !transaction.getChangeDescription().isEmpty();
}
HTH,
Bob
|
|
|
Powered by
FUDForum. Page generated in 0.03971 seconds