通过Application Module可以调用getDBTransaction().commit()来提交数据,实际上Application Module中还有另外一个方法:getTransaction()。
实际使用时,一般都直接使用getDBTransaction(),因此一直有一个疑问:如果使用getTransaction().commit(),数据库是否也提交了?
从API上来看,DBTransaction和Transaction都是接口,DBTransaction是Transaction的子类。
Transaction主要提供了一些事务常用到的方法:
(1)commit: Commits the transaction; saves all changes to the database. If the database connection is established, the transaction is implicitly started.
(2)connect: Attempts to establish a connection to the given database URL.
(3)disconnect: Disconnects the server from the database.
(4)getLockingMode: Gets the preferred locking mode for this transaction.
In ADF BC, the locking mode defaults to LOCK_PESSIMISTIC; however, Oracle Fusion applications use optimistic locking by default.
(5)rollback: Rolls back the transaction; discards all changes
(6)setLockingMode: Sets the preferred locking mode for this transaction. Changing the locking mode affects only subsequent locks that are placed.
DBTransaction继承了以上方法,并提供了一些针对EO的方法:
(1)findByPrimaryKey()
(2)getSession()
(3)getEnvironment()
实验主要步骤如下:
1. 根据表Jobs,建立相应的EO,VO,AM。
2. 定制化VO,AM,为AM类增加两个方法:createJob() 和deleteJob()
(1)createJob() 方法
public void createJob() {
JobsViewImpl jobVO = (JobsViewImpl)getJobsView1();
JobsViewRowImpl newJob = (JobsViewRowImpl)jobVO.createRow();
jobVO.insertRow(newJob);
newJob.setJobId("Oracle");
newJob.setJobTitle("CEO");
newJob.setMinSalary(new Integer(10000));
newJob.setMaxSalary(new Integer(50000));
// newJob.setMinSalary(new oracle.jbo.domain.Number(10000));
// newJob.setMaxSalary(new oracle.jbo.domain.Number(50000));
getTransaction().commit();
//getDBTransaction().commit();
}
(2)deleteJob() 方法
public void deleteJob() {
JobsViewImpl jobVO = (JobsViewImpl)getJobsView1();
JobsViewRowImpl currentJob = (JobsViewRowImpl)jobVO.getCurrentRow();
jobVO.removeCurrentRow();
getTransaction().commit();
//getDBTransaction().commit();
}
(3)saveJob() 方法
public void saveJob() {
JobsViewImpl jobVO = (JobsViewImpl)getJobsView1();
JobsViewRowImpl currentJob = (JobsViewRowImpl)jobVO.getCurrentRow();
getTransaction().commit();
//getDBTransaction().commit();
}
3. 创建页面,拖放JobsView1 Data Control,选择生成Form;再增加三个Button:Create、Delete和Save。
完整的Managed Bean代码如下:
package view;
import model.AppModuleImpl;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCDataControl;
import oracle.jbo.ApplicationModule;
import view.util.ADFUtils;
public class MyBackingBean {
public MyBackingBean() {
}
public String delete_action() {
// DCDataControl dc = BindingContext.getCurrent().getDefaultDataControl();
// ApplicationModule am = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = ADFUtils.getDCBindingContainer().getDataControl().getApplicationModule();
AppModuleImpl service = (AppModuleImpl)am;
service.deleteJob();
return null;
}
public String create_action() {
// DCDataControl dc = BindingContext.getCurrent().getDefaultDataControl();
// ApplicationModule am = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = ADFUtils.getDCBindingContainer().getDataControl().getApplicationModule();
AppModuleImpl service = (AppModuleImpl)am;
service.createJob();
return null;
}
public String save_action() {
// DCDataControl dc = BindingContext.getCurrent().getDefaultDataControl();
// ApplicationModule am = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = ADFUtils.getDCBindingContainer().getDataControl().getApplicationModule();
AppModuleImpl service = (AppModuleImpl)am;
service.saveJob();
return null;
}
}
4. 分别测试getTransaction().commit() 和getDBTransaction().commit()。
进入到数据库中查看,两次测试记录都被成功创建、删除和保存。
5. 结论
(1)使用getTransaction().commit()和getDBTransaction().commit(),数据库都提交了。
(2)一般情况下使用Transaction就可以了,除非你需要使用DBTransaction上的方法。
Project 下载:ADF_AM_Transaction_DBTransaction.7z
没有评论:
发表评论