JDBCJournal code example

Once the journal schema is created, as in the JDBCJournalSchemaGenerator code example, the applications that request the journal services to access the schema tables can be started. Before requesting the database connection, an application must take care of registering and loading the specific JDBC driver needed to work with the database, according to the database environment.

The following is a sample of an application that works with the journal service provided by the toolkit, and the definition files needed to run it. The sample is implemented for a local environment, but it can be easily adapted to a client/server environment by implementing the toolkit client operation that will start the server operation calling for the journal services.

DSEDATA.XML
< !Operation data definition that will be used by the journal service-->
<field id="branchNumber"/>
<field id="agreementNumber"/>
<kColl id="accountNumber"/>
    <refData refId="branchNumber"/>
    <refData refId="agreementNumber"/>
</kColl>
<kColl id="myOperationData"/>
    <refData refId="accountNumber"/>
    <field id="dueDate"/>
</kColl>
DSECTXT.XML
<! Contexts definition sample file >
<context id="myOperationContext" type="op"/>
    <refKColl refId="myOperationData"/>
    </refKColl>
</context>
DSESRVCE.XML
<! Services definition sample file >
<JDBCJournal id="myJournalName" autoCommit="true"
            schema="DSESCHEM"/>
    <column id="BRANCHNUMBER"
            dataName="accountNumber.branchNumber"/>
    <column id="AGREEMENTNUMBER"
            dataName="accountNumber.agreementNumber"/>
    <column id="DUEDATE" dataName="dueDate"/>
</JDBCJournal>

The column tag is used to map a specific data field in the operation context to a column in the database table, and is necessary if the data field and the column do not have the same name. When an operation to the database is run, the journal instance needs to relate the data fields that are in the operation context with the columns that are defined in the database table that the journal is working with.

The way to set this relationship is by name. By default, the journal instance expects the data fields in the operation context to have the same name as the corresponding columns in the database table. Then, if a record in the database is going to be updated with a column called BRANCHNUMBER, the journal instance will try to get the value to be inserted in the record from a data field called 'BRANCHNUMBER' in the operation context. The journal instance calls the method getValueAt('BRANCHNUMBER'). In order to modify this default behavior, the journal service allows the application to define a different relationship between data fields and the columns by using the column tag. Using the previous example, the journal instance will try to get the new value for column BRANCHNUMBER in a data field named 'accountNumber.branchNumber', by calling the method getValueAt('accountNumber.branchNumber').

The dataName tag attribute can directly hold the data field name or a key name if a KeyedObject format is being used inside the Hashtable format definition.

DSEFMTS.XML
<! Formats definition sample file >
<fmtDef id="journalFormatName"/>
    <hashtable>
        <fObject dataName="accountNumber.branchNumber"/>
        <fObject dataName="accountNumber.agreementNumber"/>
        <fObject dataName="dueDate"/>
    </hashtable>
</fmtDef>

With the previous XML definitions, the data fields that will be stored in the database and managed by the journal service are the branchNumber and the agreementNumber from the accountNumber keyed collection and the dueDate. The mapping to the DB2® columns is set in the DSESRVCE.XML file. The table has been previously created by the database administration application, using the JDBCJournalSchemaGenerator.

Application flow
Context jctx;
JournalService journal;
HashtableFormat journalFormat;

//Sets the toolkit environment.

if(!InitManager.isInitialized()){
				InitManager.reset("file:///c:\\btt\\btt.xml");
			}

//Instantiates the context and the journal service. 

jctx=ContextFactory.createContext("myOperationContext");
journal=(JournalService) Service.readObject("myJournalName"); 

//Initializes and opens the journal service.

journal.loadDriver("COM.ibm.db2.jdbc.net.DB2Driver");
journal.connect("jdbc:db2://hostname:8888/dbname","user","passw");
journal.openForEntity( "UserA"); 

//Initializes the fields to be stored in the journal. 

jctx.setValueAt("accountNumber.branchNumber","1234");
jctx.setValueAt("accountNumber.agreementNumber",new Integer(102));
jctx.setValueAt("dueDate",new java.sql.Date(98,5,22)); 

//Adds two records (with the same contents).

journal.addRecord(jctx,"journalFormatName");
journal.addRecord(jctx,"journalFormatName"); 

//Updates the first record with a new date.

jctx.setValueAt("dueDate",new java.sql.Date(99,6,1));
journal.updateRecord(1, jctx, "journalFormatName"); 

//Retrieves the second record (the last one) and updates the 
//context with the retrieved information.
//The dueDate data field value has to be again 22/06/98.

journalFormat=(HashtableFormat)FormatElement.readObject(
    "journalFormatName");
journalFormat.unformat(journal.retrieveLastRecord(),jctx);
java.util.Date myDate;
myDate=(java.util.Date)jctx.getValueAt("dueDate");
System.out.println(myDate.toString());

//After any journal updating, the changes to the database must be
//explicitly committed (if autoCommit is set to false).

journal.commit();

//When the application is done with the database, the connection
//and the current journal have to be closed. 

journal.disconnect();
journal.close();