3.1.3. Last resource commit optimisation

3.1.3. Last resource commit optimisation

In some cases it may be necessary to enlist participants that aren’t two-phase commit aware into a two-phase commit transaction. If there is only a single resource then there is no need for two-phase commit. However, what if there are multiple resources in the transaction? In this case, the Last Resource Commit optimization (LRCO) comes into play. It is possible for a single resource that is one-phase aware (i.e., can only commit or roll back, with no prepare), to be enlisted in a transaction with two-phase commit aware resources. The coordinator treats the one-phase aware resource slightly differently, in that it executes the prepare phase on all other resource first, and if it then intends to commit the transaction it passes control to the one-phase aware resource. If it commits, then the coordinator logs the decision to commit and attempts to commit the other resources as well.

In order to utilise the LRCO, your participant must implement the com.arjuna.ats.arjuna.coordinator.OnePhase interface and be registered with the transaction through the BasicAction.add operation; since this operation expects instances of AbstractRecord, you must create an instance com.arjuna.ats.arjuna.LastResourceRecord and give your participant as the constructor parameter, as shown below:

try
                {
                boolean success = false;
                AtomicAction A = new AtomicAction();
                OnePhase opRes = new OnePhase();  // used OnePhase interface
                
                System.err.println("Starting top-level action.");
                
                A.begin();
                A.add(new LastResourceRecord(opRes));
                A.add(new ShutdownRecord(ShutdownRecord.FAIL_IN_PREPARE));
                
                A.commit();