com.j_spaces.javax.cache
Interface CacheBulk

All Known Implementing Classes:
HibernateCacheStoreImpl

Deprecated.

public interface CacheBulk

The CacheBulk interface provides bulk operations support when the space interact with external data source.

The CacheBulk implementation providing Write-Behind behavior for the space when used with the Mirror Service. Whenever an object is written into space or removed from space , the CacheBulk.store(java.util.List) is called to perform the operation in the secondary storage , external data source or remote site. When the CacheBulk is used to persist data - you may use the same database transaction with the CacheBulk.BulkEntry collection list passed into CacheBulk.store(java.util.List).
The CacheBulk is used for:
1. Write-Through behavior - The space interacting directly with external data-source to persist transactional space operations. Once the commit is called the CacheBulk.store(java.util.List) includes the transaction elements with the relevant operation information.
2. Write-Behind behavior - The space replicating its operations into the Mirror Service. The Mirror Service calling the CacheBulk.store(java.util.List) to allow the user to persist the data or synchronize it with external data sources (database, remote site).

The CacheBulk.store(java.util.List) method is invoked whenever any of the following methods are called when using LocalTransactionManager or TransactionManager: 
JavaSpace API:
JavaSpace.write(Entry entry, Transaction transaction, long lease) 
JavaSpace.take(Object entry, Transaction transaction, long timeout) 
JavaSpace.takeIfExists(Object entry, Transaction transaction, long timeout) 
IJSpace.write(Object entry, Transaction transaction, long lease)  
IJSpace.update(Object updatedEntry, Transaction transaction, long lease, long timeout)  
IJSpace.update(Entry updatedEntry, Transaction transaction, long lease, long timeout)  
IJSpace.update(Object updatedEntry, Transaction transaction, long lease, long timeout, int updateModifier)  
IJSpace.update(Entry updatedEntry, Transaction transaction, long lease, long timeout, int updateModifiers) 
IJSpace.update(Entry template, Entry newEntry, Transaction txn, long lease) 
IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease) 
IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease, int modifiers) 
IJSpace.writeMultiple(Entry[] entries, Transaction txn, long lease) 
IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases, int updateModifiers)   
IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases)  
IJSpace.takeMultiple(Entry template, Transaction txn, int maxEntries) 
IJSpace.takeMultiple(Object template, Transaction txn, int maxEntries) 

Map API: IMap.put(Object key, Object value) IMap.putAll(Map collection) IMap.remove(Object key)
The CacheBulk is configured similar to the CacheLoader or the CacheStore implementations. See CacheLoader or the CacheStore for details.

Since:
5.01

Nested Class Summary
static interface CacheBulk.BulkEntry
          Deprecated. Interface for an entry involved in a bulk operation.
 
Field Summary
static short ERASE
          Deprecated. Erase operation
static short STORE
          Deprecated. Store operation
 
Method Summary
 void store(List<CacheBulk.BulkEntry> bulk)
          Deprecated. This method is called in the following scenarios: 1.
 

Field Detail

ERASE

static final short ERASE
Deprecated. 
Erase operation

See Also:
Constant Field Values

STORE

static final short STORE
Deprecated. 
Store operation

See Also:
Constant Field Values
Method Detail

store

void store(List<CacheBulk.BulkEntry> bulk)
Deprecated. 
This method is called in the following scenarios:

1. Transaction commit
2. Asynchronize persistency mode using the Mirror Service This method can throw RuntimeException. This exception will be delivered to the client application wrapped by com.j_spaces.javax.cache.CacheException

Parameters:
bulk - The bulk list contains list of BulkEntry elements
Implementation Example:

        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();
        }