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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值