Skip to main content

Logging transactions in the new vdmspace

· One min read
Alejandro Revilla
jPOS project founder

As a follow-up to the previous post about vdmspace, I've implemented a simple SpaceWriter TransactionParticipant that can be used to log all transaction contexts to the highly reliable VoldemortSpace (vdm:space). It can be added to your transaction flow like this:

The code looks like this:

package org.jpos.transaction;

import java.io.Serializable;
import org.jpos.space.Space;
import org.jpos.space.SpaceFactory;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;

public class SpaceWriter implements AbortParticipant, Configurable {
Space sp;
String prefix;
Configuration cfg;
public int prepare (long id, Serializable o) {
return PREPARED | READONLY;
}
public int prepareForAbort (long id, Serializable o) {
return PREPARED | READONLY;
}
public void commit (long id, Serializable o) {
outContext (id, (Context) o);
}
public void abort (long id, Serializable o) {
outContext (id, (Context) o);
}
private void outContext (long id, Context ctx) {
if (sp == null)
sp = SpaceFactory.getSpace (cfg.get ("space", null));
sp.out (prefix + Long.toString (id), ctx);
}
public void setConfiguration (Configuration cfg) {
this.cfg = cfg;
prefix = cfg.get ("prefix", "");
}
}

You can add this participant to the very end of your transaction flow. All persistent entries in your Context would get stored.