Java Code Examples for bitronix.tm.TransactionManagerServices#getTransactionManager()

The following examples show how to use bitronix.tm.TransactionManagerServices#getTransactionManager() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicroserviceResourceProducerTest.java    From genericconnector with Apache License 2.0 6 votes vote down vote up
@Test
public void testFind() throws ResourceException, NotSupportedException, SystemException {
	MicroserviceResourceFactory msrFactory = mock(MicroserviceResourceFactory.class);
	MicroserviceXAResource xa = new MicroserviceXAResource("a", mock(CommitRollbackCallback.class));
	when(msrFactory.build()).thenReturn(xa);
	MicroserviceResourceProducer.registerMicroserviceResourceFactory("a", msrFactory);
	MicroserviceResourceProducer producer = MicroserviceResourceProducer.getProducers().values().iterator().next();

       BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
       try{
       	tm.begin();

       	//TEST
       	producer.getTransactionAssistant(); //enlists resource into TX and means we can then go find it
       	
       	XAResource found = producer.findXAResourceHolder(xa).getXAResource();
       	assertEquals(xa, found);
       }finally{
       	tm.rollback();
       }
}
 
Example 2
Source File: XA107Test.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testXAWorksWithJsr107() throws Exception {
  BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();
  URI uri = getClass().getResource("/configs/simple-xa.xml").toURI();
  CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(uri, getClass().getClassLoader());

  Cache<String, String> xaCache = cacheManager.getCache("xaCache", String.class, String.class);

  transactionManager.begin();
  {
      xaCache.put("key", "one");
  }
  transactionManager.commit();

  cacheManager.close();
}
 
Example 3
Source File: StatefulSerializerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testXAWithStatefulSerializer() throws Exception {
  BitronixTransactionManager manager = TransactionManagerServices.getTransactionManager();
  try (CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .using(new LookupTransactionManagerProviderConfiguration(
          BitronixTransactionManagerLookup.class))
        .withCache("xaCache",
          CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Long.class, Person.class,
              ResourcePoolsBuilder.heap(5))
            .withExpiry(ExpiryPolicyBuilder.noExpiration()).withService(new XAStoreConfiguration("xaCache"))
            .build())
        .build(true)) {

    Cache<Long, Person> cache = cacheManager.getCache("xaCache", Long.class, Person.class);
    manager.begin();
    cache.put(1L, new Person("James", 42));
    manager.commit();

    manager.begin();
    assertNotNull(cache.get(1L));
    manager.commit();
  } finally {
    manager.shutdown();
  }
}
 
Example 4
Source File: UnSupportedCombinationsWithClusteredCacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClusteredCacheWithXA() throws Exception {
  TransactionManagerServices.getConfiguration().setJournal("null");

  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager();

  try {
    CacheManagerBuilder.newCacheManagerBuilder()
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application")).autoCreate(c -> c))
        .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB))
            )
                .withService(new XAStoreConfiguration("xaCache"))
                .build()
        )
        .build(true).close();
    fail("Expected StateTransitionException");
  } catch (StateTransitionException e) {
    assertThat(e.getCause().getCause().getMessage(), is("Unsupported resource type : interface org.ehcache.clustered.client.config.DedicatedClusteredResourcePool"));
  }

  transactionManager.shutdown();
}
 
Example 5
Source File: MicroserviceResourceProducerTest.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
@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();

       	//TEST
   		TransactionAssistant ta = producer.getTransactionAssistant();
   		assertNotNull(ta);
   		
   		//check its enlisted in TX
   		assertEquals(1, TransactionContextHelper.currentTransaction().getEnlistedResourcesUniqueNames().size());
   		assertEquals("a", TransactionContextHelper.currentTransaction().getEnlistedResourcesUniqueNames().iterator().next());
   		
       	//TEST
   		ta.close();

   		//cannot check its delisted from TX, because that happens during deconstruction of the transaction, becuase the TX is a global one
   		
   		//close also removed it from the holders - so check its gone
   		assertNull(producer.findXAResourceHolder(xa));
       }finally{
       	tm.rollback();
       }
}
 
Example 6
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUpBtmConfig() throws Exception {
  storagePath = folder.newFolder();

  initTransactionManagerServices();

  transactionManager = TransactionManagerServices.getTransactionManager();
}
 
Example 7
Source File: BitronixTransactionManagerLookup.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionManagerWrapper lookupTransactionManagerWrapper() { // <2>
  if (!TransactionManagerServices.isTransactionManagerRunning()) { // <3>
    throw new IllegalStateException("BTM must be started beforehand");
  }
  TransactionManagerWrapper tmWrapper = new TransactionManagerWrapper(TransactionManagerServices.getTransactionManager(),
      new BitronixXAResourceRegistry()); // <4>
  LOGGER.info("Using looked up transaction manager : {}", tmWrapper);
  return tmWrapper;
}
 
Example 8
Source File: XAGettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleXACache() throws Exception {
  // tag::testSimpleXACache[]
  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager(); // <1>

  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) // <2>
      .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, // <3>
                                                          ResourcePoolsBuilder.heap(10)) // <4>
          .withService(new XAStoreConfiguration("xaCache")) // <5>
          .build()
      )
      .build(true);

  Cache<Long, String> xaCache = cacheManager.getCache("xaCache", Long.class, String.class);

  transactionManager.begin(); // <6>
  {
    xaCache.put(1L, "one"); // <7>
  }
  transactionManager.commit(); // <8>

  cacheManager.close();
  transactionManager.shutdown();
  // end::testSimpleXACache[]
}
 
Example 9
Source File: XAGettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonTransactionalAccess() throws Exception {
  // tag::testNonTransactionalAccess[]
  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager(); // <1>

  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) // <2>
      .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, // <3>
                                                          ResourcePoolsBuilder.heap(10)) // <4>
          .withService(new XAStoreConfiguration("xaCache")) // <5>
          .build()
      )
      .build(true);

  Cache<Long, String> xaCache = cacheManager.getCache("xaCache", Long.class, String.class);

  try {
    xaCache.get(1L); // <6>
    fail("expected XACacheException");
  } catch (XACacheException e) {
    // expected
  }

  cacheManager.close();
  transactionManager.shutdown();
  // end::testNonTransactionalAccess[]
}
 
Example 10
Source File: XAGettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testXACacheWithWriteThrough() throws Exception {
  // tag::testXACacheWithWriteThrough[]
  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager(); // <1>

  Class<CacheLoaderWriter<?, ?>> klazz = (Class<CacheLoaderWriter<?, ?>>) (Class) (SampleLoaderWriter.class);

  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) // <2>
      .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, // <3>
                                                          ResourcePoolsBuilder.heap(10)) // <4>
              .withService(new XAStoreConfiguration("xaCache")) // <5>
              .withService(new DefaultCacheLoaderWriterConfiguration(klazz, singletonMap(1L, "eins"))) // <6>
              .build()
      )
      .build(true);

  Cache<Long, String> xaCache = cacheManager.getCache("xaCache", Long.class, String.class);

  transactionManager.begin(); // <7>
  {
    assertThat(xaCache.get(1L), equalTo("eins")); // <8>
    xaCache.put(1L, "one"); // <9>
  }
  transactionManager.commit(); // <10>

  cacheManager.close();
  transactionManager.shutdown();
  // end::testXACacheWithWriteThrough[]
}
 
Example 11
Source File: XAGettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testXACacheWithThreeTiers() throws Exception {
  // tag::testXACacheWithThreeTiers[]
  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager(); // <1>

  PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) // <2>
      .with(CacheManagerBuilder.persistence(new File(getStoragePath(), "testXACacheWithThreeTiers"))) // <3>
      .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, // <4>
              ResourcePoolsBuilder.newResourcePoolsBuilder() // <5>
                      .heap(10, EntryUnit.ENTRIES)
                      .offheap(10, MemoryUnit.MB)
                      .disk(20, MemoryUnit.MB, true)
              )
              .withService(new XAStoreConfiguration("xaCache")) // <6>
              .build()
      )
      .build(true);

  Cache<Long, String> xaCache = persistentCacheManager.getCache("xaCache", Long.class, String.class);

  transactionManager.begin(); // <7>
  {
    xaCache.put(1L, "one"); // <8>
  }
  transactionManager.commit(); // <9>

  persistentCacheManager.close();
  transactionManager.shutdown();
  // end::testXACacheWithThreeTiers[]
}
 
Example 12
Source File: XAGettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testXACacheWithXMLConfig() throws Exception {
  // tag::testXACacheWithXMLConfig[]
  BitronixTransactionManager transactionManager =
      TransactionManagerServices.getTransactionManager(); // <1>

  URL myUrl = this.getClass().getResource("/docs/configs/xa-getting-started.xml"); // <2>
  Configuration xmlConfig = new XmlConfiguration(myUrl); // <3>
  CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig); // <4>
  myCacheManager.init();

  myCacheManager.close();
  transactionManager.shutdown();
  // end::testXACacheWithXMLConfig[]
}
 
Example 13
Source File: XmlConfigTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleConfig() throws Exception {
  TransactionManagerServices.getConfiguration().setJournal("null").setServerId("XmlConfigTest");
  BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

  final URL myUrl = this.getClass().getResource("/configs/simple-xa.xml");
  Configuration xmlConfig = new XmlConfiguration(myUrl);
  CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
  myCacheManager.init();

  myCacheManager.close();
  transactionManager.shutdown();
}
 
Example 14
Source File: JTATransactionManagerConfiguration.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "shutdown")
@DependsOn(value = "btmConfig")
public BitronixTransactionManager jtaTransactionManager() {
    return TransactionManagerServices.getTransactionManager();
}
 
Example 15
Source File: AbstractJTATransactionManagerConfiguration.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "shutdown")
@DependsOn(value = "btmConfig")
public BitronixTransactionManager jtaTransactionManager() {
    return TransactionManagerServices.getTransactionManager();
}
 
Example 16
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoveryAfterCrash() throws Exception {
  CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      newResourcePoolsBuilder()
              .heap(10, EntryUnit.ENTRIES)
              .offheap(10, MemoryUnit.MB)
              .disk(20, MemoryUnit.MB, true));

  cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .with(new CacheManagerPersistenceConfiguration(getStoragePath()))
      .withCache("txCache1", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache1")).build())
      .withCache("txCache2", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache2")).build())
      .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
      .build(true);

  Cache<Long, String> txCache1 = cacheManager.getCache("txCache1", Long.class, String.class);
  Cache<Long, String> txCache2 = cacheManager.getCache("txCache2", Long.class, String.class);

  transactionManager.begin();
  {
    txCache1.put(1L, "one");
    txCache2.put(1L, "un");
  }
  transactionManager.getCurrentTransaction().addTransactionStatusChangeListener((oldStatus, newStatus) -> {
    if (newStatus == Status.STATUS_COMMITTING) {
      throw new AbortError();
    }
  });
  try {
    transactionManager.commit();
    fail("expected AbortError");
  } catch (AbortError e) {
    // expected
  }

  cacheManager.close();
  txCache1 = null;
  txCache2 = null;
  transactionManager.shutdown();

  initTransactionManagerServices();
  transactionManager = TransactionManagerServices.getTransactionManager();
  cacheManager.init();

  txCache1 = cacheManager.getCache("txCache1", Long.class, String.class);
  txCache2 = cacheManager.getCache("txCache2", Long.class, String.class);

  transactionManager.begin();
  {
    assertThat(txCache1.get(1L), equalTo("one"));
    assertThat(txCache2.get(1L), equalTo("un"));
  }
  transactionManager.commit();
}