MicroserviceResourceProducer extends ResourceBean implements XAResourceProducer, BasicTransactionAssistanceFactory { @Override public TransactionAssistant getTransactionAssistant() throws ResourceException { final MicroserviceXAResource microserviceResource = msrFactory.build(); final MicroserviceResourceHolder resourceHolder = new MicroserviceResourceHolder(microserviceResource, this); resourceHolders.put(resourceHolder.getXAResource(), resourceHolder); try { TransactionContextHelper.enlistInCurrentTransaction(resourceHolder); } catch (Exception e) { throw new ResourceException("Unable to enlist resource into transaction", e); } return new TransactionAssistant() { @Override public <T> T executeInActiveTransaction(ExecuteCallback<T> c) throws Exception { return microserviceResource.executeInActiveTransaction(c); } @Override public void close() { try { TransactionContextHelper.delistFromCurrentTransaction(resourceHolder); } catch (SystemException e) { throw new RuntimeException(e); } resourceHolders.remove(resourceHolder.getXAResource()); } }; } private  MicroserviceResourceProducer(); static void registerMicroserviceResourceFactory(String uniqueName, MicroserviceResourceFactory msrFactory); static void unregisterMicroserviceResourceFactory(String uniqueName); @Override XAResourceHolderState startRecovery(); @Override void endRecovery(); @Override void setFailed(boolean failed); @Override MicroserviceResourceHolder findXAResourceHolder(XAResource xaResource); @Override void init(); @Override void close(); @Override MicroserviceResourceHolder createPooledConnection(Object xaFactory, ResourceBean bean); @Override Reference getReference(); @Override String toString(); @Override TransactionAssistant getTransactionAssistant();  }
@Test public void testGetTransactionAssistant() throws ResourceException, NotSupportedException, SystemException { MicroserviceResourceFactory msrFactory = mock(MicroserviceResourceFactory.class); MicroserviceXAResource xa = new MicroserviceXAResource("a", null); when(msrFactory.build()).thenReturn(xa); MicroserviceResourceProducer.registerMicroserviceResourceFactory("a", msrFactory); MicroserviceResourceProducer producer = MicroserviceResourceProducer.getProducers().values().iterator().next(); BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager(); try{ tm.begin(); TransactionAssistant ta = producer.getTransactionAssistant(); assertNotNull(ta); assertEquals(1, TransactionContextHelper.currentTransaction().getEnlistedResourcesUniqueNames().size()); assertEquals("a", TransactionContextHelper.currentTransaction().getEnlistedResourcesUniqueNames().iterator().next()); ta.close(); assertNull(producer.findXAResourceHolder(xa)); }finally{ tm.rollback(); } }