ADF Faces - How To Get a DBTransaction Object Anytime You Need One
If you decide to execute a SQL statement or some PL/SQL code from with your ADF Faces application outside of the ADF BC framework (i.e. without using Entity Objects and View Object), you will need access to a JDBC connection. Do you need to instantiate your own connections, or maintain a separate connection pool? The answer is no! ADF BC provides us with (abstracted) access to the JDBC connection object that is associated with our current application session. Access is provided through the oracle.jbo.server.DBTransaction class. You can get an instance of the DBTransaction object from several places within the ADF BC Framework: ViewObjectImpl, TransactionEventImpl, EntityImpl, and ApplicationModuleImpl. All of these classes have a method called getDBTransaction() that return an instance of the DBTransaction object. Armed with this knowledge, how do we go about getting an instance of DBTransaction anytime we want one? By "anytime time we want one", I mean externally from a View Object or Entity Object instance. The answer is pretty simple. We just need to access to an instance of the current ApplicationModule. Using the following code, you can get a DBTransaction object anytime you want:
/*** * This method returns the current instance of the session DBTransaction object. * The method below is implemented from a Singleton Object for utilitarian purposes. */ public static synchronized DBTransaction getDBTransaction(){ FacesContext ctx = FacesContext.getCurrentInstance(); ValueBinding vb = ctx.getCurrentInstance().getApplication().createValueBinding("#{data}"); BindingContext bc = (BindingContext)vb.getValue(ctx.getCurrentInstance());
//Use your own data control name when creating the DataControl object //Look in the DataBindings.cpx file ... the id attribute of the BC4JDataControl tag DataControl dc = bc.findDataControl("MyApplicationModuleControl"); ApplicationModuleImpl am = ((ApplicationModuleImpl)(ApplicationModule)dc.getDataProvider()); return am.getDBTransaction(); }
Place the code listed above in a utility class. I prefer to use a static class or Singleton.
8:56:17 PM
|