How To Get a DBTransaction Object Anytime You Need One

http://www.jasonsdevelopercorner.com/?p=58

How To Get a DBTransaction Object Anytime You Need One

(Originally posted on the “old” Jason Bennett’s Developer Corner, Thursday, March 06, 2008)

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();

}

猜你喜欢

转载自e-soft.iteye.com/blog/1323192
今日推荐