22 June 2009

Access DBTransaction from ManagedBean in Oracle ADF

Oracle ADF - How to access DBTransaction from ManagedBean



While working on ADF project , there may be situation when we have to depend on plain old SQL query - for doing a specific job , and need to execute that SQL query right from the managed bean.


For executing the query we need to create a statement, and we have to access DBTransaction from within a managed bean.

Following code snippets will access DBTransaction in ManagedBean



There are two ways to access the DBTransaction from managed bean


situation one is, You have ADFUtils classes

You can Borrow ADFUtils classes from SRDemo application, which you can freely download,
ADFUtils and JSFUtils are extremely useful.


ApplicationModuleImpl am = (ApplicationModuleImpl)ADFUtils.getApplicationModuleForDataControl("myServiceDataControl");
Statement stmt = am.getDBTransaction().createStatement(0);



The second situation is, If you don't have ADFUtils Classes


FacesContext ctx = FacesContext.getCurrentInstance();

ValueBinding vb = ctx.getCurrentInstance().getApplication().createValueBinding("#{data}");

BindingContext bc = BindingContext)vb.getValue(ctx.getCurrentInstance());

DataControl dc = bc.findDataControl("myServiceDataControl");

ApplicationModuleImpl am = ((ApplicationModuleImpl)(ApplicationModule)dc.getDataProvider());

Statement stmt = am.getDBTransaction().createStatement(0);

How to find out DataControl name :


Open the DataBindings.cpx file and see the id attribute of the BC4JDataControl tag


Use your data control name when creating the DataControl object
here in this code I am using - BC4JDataControl id="myServiceDataControl"