From f2b01a7f01eeb6d6d5c048ad58b6942576e823d6 Mon Sep 17 00:00:00 2001 From: Gary Tully Date: Thu, 25 Apr 2013 14:33:54 +0000 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-4486 - fix and test git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1475798 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/ra/ActiveMQResourceAdapter.java | 28 ++++++++----------- .../ra/ActiveMQConnectionFactoryTest.java | 13 +++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/activemq-ra/src/main/java/org/apache/activemq/ra/ActiveMQResourceAdapter.java b/activemq-ra/src/main/java/org/apache/activemq/ra/ActiveMQResourceAdapter.java index 47f0d408a7..81b47030e8 100644 --- a/activemq-ra/src/main/java/org/apache/activemq/ra/ActiveMQResourceAdapter.java +++ b/activemq-ra/src/main/java/org/apache/activemq/ra/ActiveMQResourceAdapter.java @@ -34,6 +34,7 @@ import javax.transaction.xa.XAResource; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.RedeliveryPolicy; +import org.apache.activemq.TransactionContext; import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.util.ServiceSupport; @@ -233,25 +234,20 @@ public class ActiveMQResourceAdapter extends ActiveMQConnectionSupport implement * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[]) */ public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException { - Connection connection = null; try { - connection = makeConnection(); - if (connection instanceof XAConnection) { - XASession session = ((XAConnection)connection).createXASession(); - XAResource xaResource = session.getXAResource(); - return new XAResource[] { - xaResource - }; - } - return new XAResource[] {}; + final ActiveMQConnection connection = makeConnection(); + return new XAResource[]{new LocalAndXATransaction(new TransactionContext(connection)) { + public void finalize() throws Throwable { + try { + connection.close(); + } catch (Throwable ignore) { + } finally { + super.finalize(); + } + } + }}; } catch (JMSException e) { throw new ResourceException(e); - } finally { - try { - connection.close(); - } catch (Throwable ignore) { - // - } } } diff --git a/activemq-ra/src/test/java/org/apache/activemq/ra/ActiveMQConnectionFactoryTest.java b/activemq-ra/src/test/java/org/apache/activemq/ra/ActiveMQConnectionFactoryTest.java index 3e15a4493d..b677170d75 100644 --- a/activemq-ra/src/test/java/org/apache/activemq/ra/ActiveMQConnectionFactoryTest.java +++ b/activemq-ra/src/test/java/org/apache/activemq/ra/ActiveMQConnectionFactoryTest.java @@ -30,6 +30,7 @@ import javax.resource.spi.UnavailableException; import javax.resource.spi.XATerminator; import javax.resource.spi.work.WorkManager; +import javax.transaction.xa.XAResource; import junit.framework.TestCase; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQTopicSubscriber; @@ -105,4 +106,16 @@ public class ActiveMQConnectionFactoryTest extends TestCase { assertEquals(0, ((ActiveMQTopicSubscriber)sub).getPrefetchNumber()); } + public void testGetXAResource() throws Exception { + + ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter(); + ra.setServerUrl(url); + ra.setUserName(user); + ra.setPassword(pwd); + + XAResource[] resoruces = ra.getXAResources(null); + assertEquals("one resource", 1, resoruces.length); + + assertEquals("no pending transactions", 0, resoruces[0].recover(100).length); + } }