|
OverviewThis section discusses client applications that use the JavaSpace API to write Entry data to a space and require the space to write the data to a different data source, which can be a database or any other type of external application. The JavaSpace API write space operation results in a call to the CacheStore Interface user implementation.
This section also includes client applications that use ExternalEntry when accessing the space. This is mostly relevant for C++ , C# , JMS and JDBC applications that implicitly use ExternalEntry objects.
Writing a Single ObjectThis section describes how to write a single Entry object to a space and to a data source.
The figure illustrates how a client application writes a single Entry object to a space and persists the data in a database or other external application.
CacheStore.store ImplementationThe CacheStore interface method store is called by the space and has the following signature key: store (IGSEntry key, IGSEntry value) When using the JavaSpace API, the two parameters in the store method are identical, both carry the object data to be written as an IGSEntry object. Object entry = getConverter.toObject(IGSEntry);
The data from the individual properties of the Entry object can now be extracted and placed into record format for writing to the database using the JDBC API. Sample CodeIn client application: JavaSpaces API Using Entry object: Lease lease = JavaSpaces.write(person(first,last, id), tnx, lease); In driver CacheStore implementation: method store: public class MyCacheStoreSpaceAPIImpl extends AbstractCacheLoader implements CacheStore{ public void store(Object key, Object value) { try { Object myObject = getObject((IGSEntry) value); Person obj = null; Connection con = getConnection(); if (myObject instanceof Person) { obj = (Person) myObject; int keyValue = obj.getId().intValue(); storePerson(keyValue, obj,con ); } con.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } private void storePerson(int key, Person person, Connection con) throws SQLException { PreparedStatement stPd = con.prepareStatement("select ID from " + tableNames.get(Person.class.getName()) + " where ID = ? "); stPd.setInt(1, key); ResultSet rs = stPd.executeQuery(); // check if we need to perform update or insert if (rs.next()) { stPd.close(); stPd = con.prepareStatement("update " + tableNames.get(Person.class.getName()) + " set FirstName =? , LastName =? where ID = ? "); stPd.setString(1, person.getFirstName()); stPd.setString(2, person.getLastName()); stPd.setInt(3, key); stPd.executeUpdate(); stPd.close(); } else { stPd.close(); stPd = con.prepareStatement("insert into " + tableNames.get(Person.class.getName()) + " (FirstName, LastName, ID) values(?,?,?) "); stPd.setString(1, person.getFirstName()); stPd.setString(2, person.getLastName()); stPd.setInt(3, key); stPd.executeUpdate(); stPd.close(); } } } Writing Multiple ObjectsThis section describes how to write multiple Entry objects to a space and to a data source.
The figure illustrates how a client application writes an Entry array object data to a space and persists the data in a database or other external application.
CacheStore.storeAll ImplementationThe CacheStore Interface method storeAll is called by the space and has the following signature: storeAll (Map<IGSEntry,IGSEntry> map) Where the argument is a map of key-value pairs of IGSEntry. Object entry = getConverter.toObject(IGSEntry)
The data from the individual properties of the Entry object can now be extracted and placed into record format for writing to the database using the JDBC API. Sample codepublic void storeAll(Map map) { Connection connection = null; try { Object key = null; Object myObject = null; Person obj = null; connection = getConnection(); connection.setAutoCommit(false); // All objects will be stored in one transaction Iterator keyIter = map.keySet().iterator(); while (keyIter.hasNext()) { key = keyIter.next(); myObject = getObject((IGSEntry) key); if (myObject instanceof Person) { obj = (Person) myObject; int keyValue = obj.getId().intValue(); storePerson(keyValue, obj, connection); } } // commit transaction connection.commit(); connection.close(); } catch (Exception e) { try { connection.rollback(); connection.close(); } catch (SQLException e1) { e1.printStackTrace(); } throw new RuntimeException(e); } } Writing Objects as Part of a TransactionThis section describes how to write multiple Entry objects to a space and to a data source as part of a transaction.
The figure illustrates how a client application writes and takes a sequence of Entry object data as part of a transaction to a space and persists the data in a database or other external application.
CacheBulk.store ImplementationThe CacheStore Interface method store is called by the space and has the following signature: store (List <BulkEntry> bulk ) The object data and the operation is extracted from the BulkEntry argument using: Entry entry = BulkEntry.getEntry(); Operation operation = BulkEntry.getOperation(); The Entry and operation data for each transaction operation can now be extracted and placed into record format for the appropriate operation to the database using the JDBC API. Sample codepublic class MyCacheStoreSpaceAPIBulkImpl extends AbstractCacheLoader implements CacheBulk{ public void store(List bulk) { Connection bulkdbConnection = null; try { bulkdbConnection = getConnection(); bulkdbConnection.setAutoCommit(false); // operations done in one transaction } catch (SQLException e1) { e1.printStackTrace(); throw new RuntimeException(e1); } Iterator iter = bulk.iterator(); Person person = null; try { while (iter.hasNext()) { BulkEntry entry = (BulkEntry) iter.next(); IGSEntry e = (IGSEntry) entry.getEntry(); if (e.getClassName().equals(Person.class.getName())) { person = (Person) getObject((IGSEntry) e); if (entry.getOperation() == CacheBulk.ERASE) { erasePerson(person.id.intValue(), bulkdbConnection); } if (entry.getOperation() == CacheBulk.STORE) { storePerson(person.id.intValue(), person, bulkdbConnection); } } } // commit transaction bulkdbConnection.commit(); bulkdbConnection.close(); } catch (Exception e) { try { bulkdbConnection.rollback(); bulkdbConnection.close(); } catch (SQLException e1) { throw new RuntimeException(e1); } } } private void storePerson(int key, Person person, Connection con) throws SQLException { PreparedStatement stPd = con.prepareStatement("select ID from " + tableNames.get(Person.class.getName()) + " where ID = ? "); stPd.setInt(1, key); ResultSet rs = stPd.executeQuery(); // check if we need to perform update or insert if (rs.next()) { stPd.close(); stPd = con.prepareStatement("update " + tableNames.get(Person.class.getName()) + " set FirstName =? , LastName =? where ID = ? "); stPd.setString(1, person.getFirstName()); stPd.setString(2, person.getLastName()); stPd.setInt(3, key); stPd.executeUpdate(); stPd.close(); } else { stPd.close(); stPd = con.prepareStatement("insert into " + tableNames.get(Person.class.getName()) + " (FirstName, LastName, ID) values(?,?,?) "); stPd.setString(1, person.getFirstName()); stPd.setString(2, person.getLastName()); stPd.setInt(3, key); stPd.executeUpdate(); stPd.close(); } } private void erasePerson(int keyValue, Connection con) throws SQLException { PreparedStatement stPd = con.prepareStatement("delete from " + tableNames.get(Person.class.getName()) + " where ID = ? "); stPd.setInt(1, keyValue); stPd.executeUpdate(); stPd.close(); } }
CacheStore.store, erase ImplementationIf the CacheBulk interface is not implemented, the CacheStore.store() and CacheStore.erase() corresponding to the appropriate operations are called once the transaction is committed: CacheStore.store (Map<IGSEntry>) CacheStore.erase (Map<IGSEntry>) Example: public void erase(Object key) { try { Object myObject = getObject((IGSEntry) key); Connection con = getConnection(); if (myObject instanceof Person) { Person obj = (Person) myObject; int keyValue = obj.getId().intValue(); erasePerson(keyValue, con); } con.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } Transient EntriesWhen using the CacheStore, you might not want to persist some Entries to the underlaying database. The CacheStore does not persist Entries that has been identified as transient. A transient entry can be constructed by extending your Entry class from com.j_spaces.core.client.MetaDataEntry, and calling the MataDataEntry.makeTransient() method when constructing the Entry. |
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.



Add Comment