Posts Tagged ‘transactions’

Detecting if there is an open transaction somewhere in the application is needed in a lot of cases. Below is the code I have used to detect a dirty transaction in my Fusion Web Application and ROLLBACK if dirty is true.

try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
BindingContext bctx = DCUtil.getBindingContext(request);
DCDataControl dc = bctx.findDataControl(“MyAppModuleDataControl”);
MyAppModuleImpl am = (MyAppModuleImpl )dc.getDataProvider();

if (am.getTransaction().isDirty()) {
am.getTransaction().rollback();
}
} catch (Exception e) {
e.printStackTrace();
}

Committing through java code in your managed bean:

DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding operationBinding = bindings.getOperationBinding(“Commit”);
operationBinding.execute();

Rollback transaction through java code in your managed bean:

DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding operationBinding = bindings.getOperationBinding(“Rollback”);
operationBinding.execute();

Pay attention to the operation binding names, if you have CRUD operations from different application modules within the same page, those names will differ (generally: Commit1, Rollback1 etc)