From commits-return-13854-apmail-jackrabbit-commits-archive=jackrabbit.apache.org@jackrabbit.apache.org Mon Jan 21 12:58:27 2013 Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B09FDD7AC for ; Mon, 21 Jan 2013 12:58:27 +0000 (UTC) Received: (qmail 94328 invoked by uid 500); 21 Jan 2013 12:58:25 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 92742 invoked by uid 500); 21 Jan 2013 12:58:20 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 92697 invoked by uid 99); 21 Jan 2013 12:58:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Jan 2013 12:58:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Jan 2013 12:58:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 46D86238896F; Mon, 21 Jan 2013 12:57:56 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1436323 - /jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java Date: Mon, 21 Jan 2013 12:57:56 -0000 To: commits@jackrabbit.apache.org From: ckoell@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130121125756.46D86238896F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ckoell Date: Mon Jan 21 12:57:55 2013 New Revision: 1436323 URL: http://svn.apache.org/viewvc?rev=1436323&view=rev Log: JCR-861 Connector should support LocalTransaction as well as XATransaction Modified: jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java Modified: jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java?rev=1436323&r1=1436322&r2=1436323&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java (original) +++ jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnection.java Mon Jan 21 12:57:55 2013 @@ -27,10 +27,14 @@ import javax.resource.spi.LocalTransacti import javax.resource.spi.ManagedConnection; import javax.resource.spi.ManagedConnectionMetaData; import javax.security.auth.Subject; +import javax.transaction.xa.XAException; import javax.transaction.xa.XAResource; +import javax.transaction.xa.Xid; + import java.io.PrintWriter; import java.util.Iterator; import java.util.LinkedList; +import java.util.concurrent.atomic.AtomicInteger; /** * This class implements the managed connection for @@ -40,6 +44,112 @@ public class JCAManagedConnection implements ManagedConnection, ManagedConnectionMetaData { /** + * The LocalTransactionAdapter wraps the internal XAResource and uses the XA Method's to + * fulfill the LocalTransaction calls. + */ + private static class LocalTransactionAdapter implements javax.resource.spi.LocalTransaction { + + /** + * Internal {@link Xid} implementation. + */ + class XidImpl implements Xid { + + private final byte[] globalTxId; + + public XidImpl(byte[] globalTxId) { + this.globalTxId = globalTxId; + } + + /** + * {@inheritDoc} + */ + public int getFormatId() { + return 0; + } + + /** + * {@inheritDoc} + */ + public byte[] getBranchQualifier() { + return new byte[0]; + } + + /** + * {@inheritDoc} + */ + public byte[] getGlobalTransactionId() { + return globalTxId; + } + } + + /** + * Global static counter for the internal Xid's + */ + private static AtomicInteger globalCounter = new AtomicInteger(); + + private XAResource resource; + private Xid xid; + + public LocalTransactionAdapter(XAResource xaResource) { + this.resource = xaResource; + } + + /** + * {@inheritDoc} + */ + @Override + public void begin() throws ResourceException { + try { + this.xid = new XidImpl(intToByteArray(globalCounter.getAndIncrement())); + resource.start(xid, XAResource.TMNOFLAGS); + } catch (XAException e) { + throw new ResourceException(e.getMessage()); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void commit() throws ResourceException { + try { + resource.end(xid, XAResource.TMSUCCESS); + resource.commit(xid, true); + } catch (XAException e) { + throw new ResourceException(e.getMessage()); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void rollback() throws ResourceException { + try { + resource.end(xid, XAResource.TMFAIL); + resource.rollback(xid); + } catch (XAException e) { + throw new ResourceException(e.getMessage()); + } + } + + /** + * Converts the given int (global transaction id) to a byte[] + * + * @param value + * @return byte[] + */ + private static byte[] intToByteArray(int value) { + byte[] b = new byte[4]; + for (int i = 0; i < 4; i++) { + int offset = (b.length - 1 - i) * 8; + b[i] = (byte) ((value >>> offset) & 0xFF); + } + return b; + } + } + + /** * Managed connection factory. */ private final JCAManagedConnectionFactory mcf; @@ -74,6 +184,8 @@ public class JCAManagedConnection */ private PrintWriter logWriter; + private LocalTransactionAdapter localTransactionAdapter; + /** * Construct the managed connection. */ @@ -86,10 +198,11 @@ public class JCAManagedConnection this.listeners = new LinkedList(); this.handles = new LinkedList(); if (this.mcf.getBindSessionToTransaction().booleanValue()) { - this.xaResource = new TransactionBoundXAResource(this, (XAResource) session); + this.xaResource = new TransactionBoundXAResource(this, (XAResource) session); } else { this.xaResource = (XAResource) session; } + this.localTransactionAdapter = new LocalTransactionAdapter(xaResource); } /** @@ -204,7 +317,7 @@ public class JCAManagedConnection */ public LocalTransaction getLocalTransaction() throws ResourceException { - throw new UnsupportedOperationException("Local transaction is not supported"); + return localTransactionAdapter; } /**