Vivian
Vivian

Reputation: 11

Abort EJB transaction from JSF UI

I am working on a project with the following technologies: Java 1.8, EJB 3, JSF (Primefaces), Oracle, Weblogic 12.2.1

I have a "Save" button which runs a time consuming EJB transaction (read, update,write in Oracle).

While the method runs the user sees a "please wait" modal dialog with a "Cancel" button. How can i abort(explicit rollback) my EJB transaction when user presses the "Cancel" button without unlocking my (Primefaces) UI?

In essence, how to rollback an already running transaction from an other method? The transactions are Container Managed Transactions.

A snippet of my code:

@Named
@ViewScoped
public class MyBean implements Serializable {

    @Inject
    private IFacade facade;

    public String save() {
        facade.save();
    }

    public void abortSave() {
        // Cancel save transaction.
    }

}

@Stateless
public class FacadeImpl implements IFacade {

    @Override
    public String save() {
        // Transaction here, read, write, update DB.
    }

}

Upvotes: 1

Views: 350

Answers (1)

commit-man
commit-man

Reputation: 376

I can imagine only one solution : You have some shared object ( field in managed JSF bean ) with volatile flag "isCancelled" ( or this shared object can be AtomicBoolean ).

You pass this object as a parameter to IFacade.save() method. In save method you can check cancelled status wherever you want and rollback transaction via EJBContext.setRollbackOnly().

Upvotes: 0

Related Questions