jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Marcin Cenkier" <Marcin.Cenk...@cognifide.com>
Subject How to integrate XAResource into Spring's JtaTransactionManager?
Date Mon, 29 Aug 2005 12:38:47 GMT
Hi,
I'm trying to use jackrabbit with JTA in Spring. For now, I wrote a
simple AOP on top of all methods that must run in separate transactions.
I reuse session after each transaction, and the code below works:

public Object invoke(MethodInvocation methodInvocation) throws Throwable
{
  Session currentSession = sessionManager.getSessionForCurrentContext();
  if (!(currentSession instanceof XASession)) {
    return methodInvocation.proceed();
  }
  XASession session = (XASession) currentSession;

  XAResource xaResource = session.getXAResource();

  final byte[] branchQualifier = new byte[8];
  final byte[] globalTransactionId = new byte[8];

  random.nextBytes(branchQualifier);  // create random id
  random.nextBytes(globalTransactionId);

  Xid xid = new Xid() {
    public byte[] getBranchQualifier() {
      return branchQualifier;
    }

    public int getFormatId() {
      return 0;
    }

    public byte[] getGlobalTransactionId() {
      return globalTransactionId;
    }
  };
  try {
    xaResource.start(xid, XAResource.TMNOFLAGS);

    Object retValue = methodInvocation.proceed();

    xaResource.end(xid, XAResource.TMSUCCESS);

    xaResource.prepare(xid);
    xaResource.commit(xid, false);

    return retValue;
  } catch (XAException e) {
    throw new RuntimeException("Cannot commit transacion.", e);
  } catch (Throwable e) {
    xaResource.end(xid, XAResource.TMFAIL);

    xaResource.prepare(xid);
    xaResource.rollback(xid);
    throw e;
  }
}

It works, but I would like to have transactions managed declaratively in
Spring configuration. In this case I need (do I?) to extend/use Spring's
JtaTransactionManager... How to do it? Maybe someone has done it
already?


Cheers,
Marcin

Mime
View raw message