Once the STORE table is created, as shown in the example using JDBCStoreSchemaGenerator (see the JDBCStoreSchemaGenerator code example), any applications that request the store services to access the database table can be started. Before requesting the database connection, all of these applications must register and load the specific JDBC driver needed to work with the database, which varies according to your database environment. Remember that the registration and loading of the appropriate JDBC driver can be done by calling a store service instance method.
The following is a sample of an application working with the Store service provided by the toolkit, and of the XML 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, which will start the server operation calling for the Store service.
<kColl id="myStoreOperationData"> <field id="account_number" /> <field id="amount" /> <field id="date" /> <field id="DESCRIPTION" /> </kColl>
<context id="myStoreContext" type="op" > <refKColl refId="myStoreOperationData"> </refKColl> </context>
<JDBCStore id="myStoreName" autoCommit="false" table="STORETABLE"> <column id="ACCOUNT_NUMBER" dataName="account_number" /> <column id="AMOUNT" dataName="amount" /> <column id="THE_DATE" dataName="date" /> </JDBCStore>
<fmtDef id="storeFormatName"> <hashtable> <fObject dataName="account_number" /> <fObject dataName="amount" /> <fObject dataName="date" /> <fObject dataName="DESCRIPTION" /> </hashtable> </fmtDef>
Application flow
import java.util.Enumeration; import java.sql.*; import com.ibm.btt.base.*; import com.ibm.btt.services.jdbc.*; public class StoreApplication { public static void main(String args[]) throws java.io.IOException, DSEObjectNotFoundException { Context storeContext; HashtableFormat storeFormat= null; StoreService store = null; // Setting the toolkit environment if not previously done if(!InitManager.isInitialized()){ InitManager.reset("file:///c:\\btt\\btt.xml"); } try { storeContext = ContextFactory.createContext("myStoreContext"); System.out.println(">>> Creating a Store Instance..."); store = (StoreService)Service.readObject("myStoreName"); // Connect to the database store.loadDriver(); store.connect("jdbc:db2://myhostname:8888/mydb", "MYUSER","MYPASSWORD"); if (store.isActive() == false) { store.open(); } int nbrOfRecords = 3; // Filling the Store Table with 3 records... for (int i = 1; i <= nbrOfRecords ; i++) { storeContext.setValueAt("account_number", "0007000" + i); storeContext.setValueAt("amount",new Integer(200000 + i)); storeContext.setValueAt("date", new java.sql.Date(98,4, 16)); storeContext.setValueAt("DESCRIPTION", "Adding record " + i + " in Store instance 1..."); storeFormat = (HashtableFormat) FormatElement.readObject("storeFormatName"); // Call the format method with argument the operation context Hashtable dataTable = new Hashtable(); dataTable = (Hashtable) storeFormat.format(storeContext); // Adding a record to the Store..."); store.addRecord(dataTable); } // End for // Application committing Database changes..."); store.commit(); // Disconnecting JDBCStore instance from Database store.disconnect(); store.close(); } // End try catch (Exception e ) {System.out.println(e.getMessage()); try { store.disconnect(); store.close(); } catch (Exception ex){System.out.println(ex.getMessage());} return; } } }
import java.util.Enumeration; import java.sql.*; import com.ibm.btt.base.*; import com.ibm.btt.services.jdbc.*; public class ForwarderApplication { public static void main(String args[]) throws java.io.IOException, DSEObjectNotFoundException { Context storeContext; HashtableFormat storeFormat= null; StoreService store = null; // Setting the toolkit environment if not previously done if(!InitManager.isInitialized()){ InitManager.reset("file:///c:\\btt\\btt.xml"); } try { storeContext = ContextFactory.createContext ("myStoreOperationContext"); System.out.println(">>> Creating a Store Instance..."); store = (StoreService)Service.readObject("myStoreName"); storeFormat = (HashtableFormat) FormatElement.readObject("storeFormatName"); // Connect to the database store.loadDriver(); store.connect("jdbc:db2://myhostname:8888/mydbe", "MYUSER","MYPASSWORD"); if (store.isActive() == false) { store.open(); } aDataHashtable = store.retrieveFirstRecordForForwarding(); // here the forwarder application will send the pending // operation to be executed. The application will wait // for the operation execution result; if the operation was // successfully executed, then it will commit the changes in the database store.commit(); // if the operation was not successfully executed, then it // is the forwarder's responsibility to keep this operation // information and retry later or, if it is because the session // with the host is down again, the application can roll back // the database changes // store.rollback(); // the process will be repeated until the last database record // is retrieved while ( (aDataHashtable=store.retrieveNextRecordForForwarding()) != null) { //do the same as for the first record } // End while store.deleteAllRetrievedForForwarding(); store.commit(); // Disconnecting JDBCStore instance from Database store.disconnect(); store.close(); } // End try catch (Exception e ) {System.out.println(e.getMessage()); try { store.disconnect(); store.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } return } } }