The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part IV
Explore Jini Transactions with JavaSpaces
by Eric Freeman and Susan Hupfer
First Published in JavaWorld, April 2000

<<  Page 3 of 5  >>


Using a Transaction Manager

To make use of transactions, you first need access to a transaction manager that can create and maintain your transactions for you. To locate a manager, you use Jini's lookup and discovery. Like all Jini services, the lookup service returns a proxy object to a transaction manager; in this specific case, you'll be looking for a service that implements the TransactionManager interface. You might want to refer to Bill Venners's previous column on lookup and discovery (see Resources) for the specifics of locating a Jini service. Here you are going to use a simple utility class from our book JavaSpaces Principles, Patterns and Practice that obtains a handle to a transaction manager proxy (please refer to the book for the details of this utility class). Here is the code you use to obtain a proxy using the utility class:

TransactionManager mgr = TransactionManagerAccessor.getManager();

Here you call the getManager static method of the TransactionManagerAccessor class, which returns a TransactionManager proxy object. With this proxy in hand, you can create a transaction that will manage a set of operations over one or more Jini services (such as a JavaSpace), which implement the TransactionParticipant interface.

Now we'll show you how to create the transaction:

Transaction.Created trc = null;
try {
trc = TransactionFactory.create(mgr, 300000);
} catch (Exception e) {
System.err.println("Could not create transaction " + e);

First you declare a variable trc of type Transaction.Created (an inner class of Transaction), which is the type of object that will be returned when you ask the transaction manager to create a new transaction (we'll return to the inner class shortly, since the syntax may be a bit confusing). To create a transaction, you then use the TransactionFactory class and call its static method create, which takes a transaction manager and a lease time (in milliseconds) as parameters and creates a transaction that the supplied manager manages for the given lease time (in this code, you should request a lease of 5 minutes). If the call to create is successful, a Transaction.Created object is returned and assigned to the variable trc. If something goes wrong during the transaction's creation, an exception is thrown instead.

Now let's revisit the Transaction.Created object, which may look odd to you. This object is simply an instantiation of the Transaction interface's public inner class, which looks like this:

public static class Created implements Serializable {
public final Transaction transaction;
public final Lease lease;

Created(Transaction transaction, Lease lease) {...}

This class is simple: it contains only two public fields and a constructor. This class is needed because the create call to the TransactionFactory needs to return two values -- the transaction itself and its granted lease time -- both of which the Created class wraps into one returned object. Once the call to create returns a Created object, you can simply access its two public fields (transaction and lease) to retrieve the respective objects. For instance, you can use the transaction field to obtain a reference to the newly created transaction object like this:

Transaction txn = trc.transaction;

Likewise, you can retrieve the transaction's lease by accessing the transaction's lease field. Note that a lease on a transaction represents the amount of time for which the transaction manager will maintain the transaction. Once a transaction expires, the transaction is aborted and all its participants are asked to roll back their state to the point before the transaction began.

<<  Page 3 of 5  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use