In order for your applications to possess all of the necessary ACID properties when using transactions, the objects manipulated by those transactions need to be both durable and isolated from conflicts. If your application objects aren't backed by a database, then you have the headache of providing these capabilities yourself, unless you have your friendly neighborhood transactional framework like TXOJ. Without going into all of the details (which really are in the documentation) probably the most important class is StateManager which provides all of the basic support mechanisms required by an object for state management purposes. As with concurrency control, which we'll come on to later, durability is obtained through inheritance.
Objects in TXOJ are assumed to be of three possible basic flavours. They may simply be recoverable, in which case StateManager will attempt to generate and maintain appropriate recovery information for the object. Such objects have lifetimes that do not exceed the application program that creates them. Objects may be recoverable and persistent, in which case the lifetime of the object is assumed to be greater than that of the creating or accessing application so that in addition to maintaining recovery information StateManager will attempt to automatically load (unload) any existing persistent state for the object at appropriate times. Finally, objects may possess none of these capabilities in which case no recovery information is ever kept nor is object activation/deactivation ever automatically attempted.
There are quite a few StateManager methods that your application could utilize directly, but for now the only three that are of immediate importance are save_state, restore_state and type. The first two methods are responsible for saving (or restoring) the state of the object, whereas the last is used when positioning the state in the transactional object store. Now although the store may also be the location of the transaction log, the application object states aren't necessarily maintained within the log. (Yes, I'm glossing over subjects such as activating and passivating objects, but we may come back to those in a later posting.)
With these three StateManager methods defined you could manage the state of your object's within the scope of a transaction. But typically you'll also want isolation, so let's move on to LockManager, which inherits from StateManager. So your application objects can become isolated and durable by having their corresponding classes inherit from LockManager. Although you have to be concerned about the three StateManager methods mentioned earlier, there's nothing else here that you need worry about in terms of overriding or defining. But of course you do need to tell the system when and how to isolate your objects. The primary programmer interface to the concurrency controller is via the setlock operation. By default the runtime enforces strict two-phase locking following a multiple reader, single writer policy on a per object basis. Although lock acquisition is under programmer control, lock release is normally under control of the system and requires no further intervention by the programmer. This ensures that the two-phase property can be correctly maintained. (Again, I'll ignore subjects like type specific concurrency control for this posting.)
But sometimes a picture paints a thousand words, so here's an example:
public class AtomicObject extends LockManager
{
public AtomicObject ()
{
super(ObjectType.ANDPERSISTENT);
state = 0;
AtomicAction A = new AtomicAction();
A.begin();
if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED)
{
if (A.commit() == ActionStatus.COMMITTED)
System.out.println("Created persistent object " + get_uid());
else
System.out.println("Action.commit error.");
}
else
{
A.abort();
System.out.println("setlock error.");
}
}
public AtomicObject (Uid u)
{
super(u, ObjectType.ANDPERSISTENT);
state = -1;
AtomicAction A = new AtomicAction();
A.begin();
if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED)
{
System.out.println("Recreated object " + u);
A.commit();
}
else
{
System.out.println("Error recreating object " + u);
A.abort();
}
}
public void incr (int value) throws TestException
{
AtomicAction A = new AtomicAction();
A.begin();
if (setlock(new Lock(LockMode.WRITE), retry) == LockResult.GRANTED)
{
state += value;
if (A.commit() != ActionStatus.COMMITTED)
throw new TestException("Action commit error.");
else
return;
}
A.abort();
throw new TestException("Write lock error.");
}
public void set (int value) throws TestException
{
AtomicAction A = new AtomicAction();
A.begin();
if (setlock(new Lock(LockMode.WRITE), retry) == LockResult.GRANTED)
{
state = value;
if (A.commit() != ActionStatus.COMMITTED)
throw new TestException("Action commit error.");
else
return;
}
A.abort();
throw new TestException("Write lock error.");
}
public int get () throws TestException
{
AtomicAction A = new AtomicAction();
int value = -1;
A.begin();
if (setlock(new Lock(LockMode.READ), retry) == LockResult.GRANTED)
{
value = state;
if (A.commit() == ActionStatus.COMMITTED)
return value;
else
throw new TestException("Action commit error.");
}
A.abort();
throw new TestException("Read lock error.");
}
public boolean save_state (OutputObjectState os, int ot)
{
boolean result = super.save_state(os, ot);
if (!result)
return false;
try
{
os.packInt(state);
}
catch (IOException e)
{
result = false;
}
return result;
}
public boolean restore_state (InputObjectState os, int ot)
{
boolean result = super.restore_state(os, ot);
if (!result)
return false;
try
{
state = os.unpackInt();
}
catch (IOException e)
{
result = false;
}
return result;
}
public String type ()
{
return "/StateManager/LockManager/AtomicObject";
}
private int state;
}
What we have here is a durable and isolatable integer. It may look like quite a bit of code, but there's a lot of defensive programming here to illustrate explicitly some of the problems that could occur. It's left as an exercise for the reader to optimize the code.
However, what you can see here is the use of AtomicAction for creating transactions (which will be automatically nested if there's already a transaction associated with the thread of control) and then the acquiring of the right types of Lock within the application object code (READ or WRITE to signify whether or not the state of the object may be modified). If you consider what's needed to ensure that the object is transactional, including full recovery, there isn't a lot for the developer to do. OK, it's a bit more than you might expect if you're used to EJB3s, for example, but then there's a bit more going on here, and some of it could well be automated (future posting hint). But when you consider that an entire database system was built on this basis back in the early 1990's (OK, the C++ version of the transaction system and not Java, but very little difference), it helps to illustrate the power of the abstractions as well as the implementation.
OK, that's enough for now. In a later post we may go into more detail on some of the concepts mentioned here (or glossed over so far) before heading off into STM land. If there's anything specific you'd like concentrating on then just suggest via the comments.
1 comment:
I just replicated this example into the JBoss TM svn repository under trunk using maven:
https://svn.jboss.org/repos/labs/labs/jbosstm/trunk/ArjunaCore/examples/txoj-stm
Hope you enjoy it!
Post a Comment