Creating and Using a Custom DBTransaction Implementation
Oracle JDeveloper Tip
Creating and Using a Custom DBTransaction ImplementationAuthor: Steve Muench, ADF Development Team Date: May 24, 2005 Revision 1.0 (Revision History)Abstract
This paper outlines the steps to create a custom DBTransactionImpl class
for use in an ADF application using ADF Business Components.
Contents Overview Creating a
DBTransaction Implementation Class Using Your DBTransaction
Implementation Class
Overview
The
DBTransaction object represents the database transaction,
interacts with the JDBC connection, and hold the entity object caches for a
root application module. Sometimes you need to customize the default behavior
of the transaction object. This brief arcticle explains how to do that and
provides a link to a downloadable example application that illustrates the
technique in action.
Creating a
DBTransaction Implementation Class
To create a custom
DBTransaction implementation class, create a new class that
extends the DBTransactionImpl2 class like this:
package demo.model;
import oracle.jbo.server.DBTransactionImpl2;
public class CustomDBTransactionImpl extends DBTransactionImpl2 {
// TODO Override methods you want to augment here
}
Once you've created the basic custom DBTransaction
implementation class, you can then use the Tools | Override
Methods... dialog to override the necessary methods that you
might want to customize. As a simple example, we could override several methods
and print a message to standard out before and after calling the superclass'
implementation like this:
package demo.model;
import oracle.jbo.server.DBTransactionImpl2;
import oracle.jbo.server.TransactionEvent;
/**
* Custom ADF DBTransaction implementation.
*
* Works in tandem with a custom DatabaseTransactionFactory implementation
* which returns instances of this subclass of DBTransactionImpl2
* instead of the default one.
*
* @author Steve Muench
*/
public class CustomDBTransactionImpl extends DBTransactionImpl2 {
/**
* This framework method is called by clients to commit any pending
* changes in the transaction. It will first post any outstanding changes
* then issue the database commit to end the transaction.
*/
public void commit() {
msg("Before commit");
super.commit();
msg("After commit");
}
/**
* This framework method is called by clients to validate all invalid
* objects in the transaction.
*/
public void validate() {
msg("Before validate");
super.validate();
msg("After validate");
}
/**
* This framework method is invoked both by the postChanges()
* and by the commit() methods to post pending transaction changes.
*
* @param te TransactionEvent object
*/
protected void postChanges(TransactionEvent te) {
msg("Before postChanges");
super.postChanges(te);
msg("After postChanges");
}
/**
* This framework method is called to actually issue the final
* 'COMMIT' statement to the database to end the transaction.
*/
protected void doCommit() {
msg("Before doCommit");
super.doCommit();
msg("After doCommit");
}
private static final String MSGPREFIX = "### CustomDBTransactionImpl: ";
private void msg(String s) {
System.out.println(MSGPREFIX + s);
}
}
Since in the ADF framework the implementation of the
DBTransaction interface is constructed by the
DatabaseTransactionFactory at runtime, we need to also
provide a customized subclass of DatabaseTransactionFactory
to cause our customized DBTranaction implemenation class to get used.
The easiest way to accomplish this is to create a new class that extends
the existing DatabaseTransactionFactory and overrides the
create() method to return a new instance of our customized
DBTransaction implementation like this:
package demo.model;
import oracle.jbo.server.DBTransactionImpl2;
import oracle.jbo.server.DatabaseTransactionFactory;
public class CustomDatabaseTransactionFactory extends DatabaseTransactionFactory {
public DBTransactionImpl2 create() {
return new CustomDBTransactionImpl();
}
}
Using Your DBTransaction
Implementation Class
Finally, in order to use your custom
DBTransaction implementation, you need to ask ADF to use
your custom DatabaseTransactionFactory class that will cause
it to be created at runtime. You do this by setting the value of the ADF
configuration property named TransactionFactory to the
fully-qualified class name of your custom
DatabaseTransactionFactory class. In our case, we'll want to
set the property as follows:
TransactionFactory = demo.model.CustomDatabaseTransactionFactory
We can do this by selecting our application
module and choosing Configurations... from its
right-mouse menu. After clicking (Edit) to edit the
YourModuleNameLocal
configuration, we visit the Properties tab and find the property to change at
the end of the alphabetical list of properties.
To verify that your
custom DBTransaction implementation is correctly setup and
being used, launch the Business Components Tester tool by
selecting Test... from the right mouse menu of your
application module in the Appliation Navigator. When the
Business Components Tester - Connect dialog appears, make
sure to explicitly pick the
YourModuleNameLocal configuration
name from the Business Component Configuration Name
drop-down list in the upper right-hand corner. Then, using the tester tool, try
to update a row in a view object and commit the change. You should see the
effects of our custom DBTransaction implementation class
printed to the Log window inside JDeveloper 10g.
| NOTE: |
You can
download a working example of this from here.
|
Revision History
| Date |
Comments |
| 24-May-2005 |
Created |
|