See the WebSphere eXtreme Scale Wiki for links to eXtreme Scale Version 7.0 documentation.
If you log in
with your developerWorks ID, you can leave comments and feedback for the development team.
A plug-in slot is a transactional storage space that is reserved for plug-ins that share transactional context. These slots provide a way for ObjectGrid plug-ins to communicate with each other, share transactional context, and ensure that transactional resources are used correctly and consistently within a transaction.
A plug-in can store transactional context, such as database connection, Java Message Service (JMS) connection, and so on, in a plug-in slot. The stored transactional context can be retrieved by any plug-in that knows the plug-in slot number, which serves as the key to retrieve transactional context.
Using plug-in slots
Plug-in slots are part of TxID
. The slots are entries on an ArrayList array. Plug-ins can reserve an entry in the ArrayList array by calling the ObjectGrid.reserveSlot method and indicating that it wants a slot on all TxID objects. Once the slots are reserved, plug-ins can put transactional context into slots of each TxID object and retrieve the context later. The put and get operations are coordinated by slot numbers that are returned by the ObjectGrid.reserveSlot method.
A plug-in usually has a life cycle. Using plug-in slots has to fit into the life cycle of plug-in. Usually, the plug-in has to reserve plug-in slots during the initialization stage and obtain slot numbers for each slot. During normal runtime, the plug-in puts transactional context into the reserved slot in TxID at the appropriate point. Usually, this is at the beginning of the transaction. The plug-in or other plug-ins can then get the stored transactional context by the slot number from the TxID within the transaction.
The plug-in usually has to clean-up at the end of the transaction. The clean-up may involve closing transactional resources. The following snippet of code illustrates how to use plug-in slots in a TransactionCallback plug-in:
public class DatabaseTransactionCallback implements TransactionCallback {
int connectionSlot;
int autoCommitConnectionSlot;
int psCacheSlot;
Properties ivProperties = new Properties();
public void initialize(ObjectGrid objectGrid) throws TransactionCallbackException {
try {
connectionSlot = objectGrid.reserveSlot(TxID.SLOT_NAME);
psCacheSlot = objectGrid.reserveSlot(TxID.SLOT_NAME);
autoCommitConnectionSlot = objectGrid.reserveSlot(TxID.SLOT_NAME);
} catch (Exception e) {
}
}
public void begin(TxID tx) throws TransactionCallbackException {
try {
Connection conn = null;
conn = DriverManager.getConnection(ivDriverUrl, ivProperties);
tx.putSlot(connectionSlot, conn);
conn = DriverManager.getConnection(ivDriverUrl, ivProperties);
conn.setAutoCommit(true);
tx.putSlot(autoCommitConnectionSlot, conn);
tx.putSlot(psCacheSlot, new HashMap());
} catch (SQLException e) {
SQLException ex = getLastSQLException(e);
throw new TransactionCallbackException("unable to get connection", ex);
}
}
public void commit(TxID id) throws TransactionCallbackException {
try {
Connection conn = (Connection) id.getSlot(connectionSlot);
conn.commit();
cleanUpSlots(id);
} catch (SQLException e) {
SQLException ex = getLastSQLException(e);
throw new TransactionCallbackException("commit failure", ex);
}
}
void cleanUpSlots(TxID tx) throws TransactionCallbackException {
closePreparedStatements((Map) tx.getSlot(psCacheSlot));
closeConnection((Connection) tx.getSlot(connectionSlot));
closeConnection((Connection) tx.getSlot(autoCommitConnectionSlot));
}
/**
* @param map
*/
private void closePreparedStatements(Map psCache) {
try {
Collection statements = psCache.values();
Iterator iter = statements.iterator();
while (iter.hasNext()) {
PreparedStatement stmt = (PreparedStatement) iter.next();
stmt.close();
}
} catch (Throwable e) {
}
}
/**
* Close connection and swallow any Throwable that occurs.
*
* @param connection
*/
private void closeConnection(Connection connection) {
try {
connection.close();
} catch (Throwable e1) {
}
}
public void rollback(TxID id) throws TransactionCallbackException
try {
Connection conn = (Connection) id.getSlot(connectionSlot);
conn.rollback();
cleanUpSlots(id);
} catch (SQLException e) {
}
}
public boolean isExternalTransactionActive(Session session) {
return false;
}
public int getConnectionSlot() {
return connectionSlot;
}
public int getAutoCommitConnectionSlot() {
return autoCommitConnectionSlot;
}
public int getPreparedStatementSlot() {
return psCacheSlot;
}
}
The following snippet of code illustrates how a Loader can get the stored transactional context that is put by previous TransactionCallback plug-in example:
public class DatabaseLoader implements Loader
{
DatabaseTransactionCallback tcb;
public void preloadMap(Session session, BackingMap backingMap) throws LoaderException
{
tcb = (DatabaseTransactionCallback)session.getObjectGrid().getTransactionCallback();
}
public List get(TxID txid, List keyList, boolean forUpdate) throws LoaderException
{
Connection conn = (Connection)txid.getSlot(tcb.getConnectionSlot());
return null;
}
public void batchUpdate(TxID txid, LogSequence sequence) throws LoaderException, OptimisticCollisionException
{
Connection conn = (Connection)txid.getSlot(tcb.getConnectionSlot());
}
}
© Copyright IBM Corporation 2007,2009. All Rights Reserved.