bitronix.tm.BitronixTransactionManager Java Examples

The following examples show how to use bitronix.tm.BitronixTransactionManager. 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: 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 #2
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 #3
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 #4
Source File: TransactionalOsgiTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static void testProgrammaticConfiguration() throws Exception {
  BitronixTransactionManager transactionManager = getTransactionManager();

  try (CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
    .withClassLoader(TestMethods.class.getClassLoader())
    .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
    .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10))
      .withService(new XAStoreConfiguration("xaCache")).build()).build(true)) {

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

    transactionManager.begin();
    try {
      xaCache.put(1L, "one");
    } catch (Throwable t) {
      transactionManager.rollback();
    }
    transactionManager.commit();
  }
  transactionManager.shutdown();
}
 
Example #5
Source File: TransactionalOsgiTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static void testXmlConfiguration() throws Exception {
  BitronixTransactionManager transactionManager = getTransactionManager();

  try (CacheManager cacheManager = newCacheManager(
    new XmlConfiguration(TestMethods.class.getResource("ehcache-xa-osgi.xml"), TestMethods.class.getClassLoader())
  )) {
    cacheManager.init();

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

    transactionManager.begin();
    try {
      xaCache.put(1L, "one");
    } catch (Throwable t) {
      transactionManager.rollback();
    }
    transactionManager.commit();
  }
  transactionManager.shutdown();
}
 
Example #6
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 #7
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private void putIfAbsentAssertions(BitronixTransactionManager transactionManager, Cache<Long, String> txCache1) throws Exception {
  transactionManager.begin();
  {
    assertThat(txCache1.putIfAbsent(1L, "one"), is(nullValue()));
    assertThat(txCache1.putIfAbsent(1L, "un"), equalTo("one"));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "one");

  transactionManager.begin();
  {
    assertThat(txCache1.putIfAbsent(1L, "eins"), equalTo("one"));
    txCache1.remove(1L);
    assertThat(txCache1.putIfAbsent(1L, "een"), is(nullValue()));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "een");
}
 
Example #8
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private void replace2ArgsAssertions(BitronixTransactionManager transactionManager, Cache<Long, String> txCache1) throws Exception {
  transactionManager.begin();
  {
    assertThat(txCache1.replace(1L, "one"), is(nullValue()));
    txCache1.put(1L, "un");
    assertThat(txCache1.replace(1L, "eins"), equalTo("un"));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "eins");

  transactionManager.begin();
  {
    assertThat(txCache1.replace(1L, "een"), equalTo("eins"));
    txCache1.put(1L, "un");
    assertThat(txCache1.replace(1L, "een"), equalTo("un"));
    txCache1.remove(1L);
    assertThat(txCache1.replace(1L, "one"), is(nullValue()));
    assertThat(txCache1.get(1L), is(nullValue()));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, null);
}
 
Example #9
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private void replace3ArgsAssertions(BitronixTransactionManager transactionManager, Cache<Long, String> txCache1) throws Exception {
  transactionManager.begin();
  {
    assertThat(txCache1.replace(1L, "one", "un"), is(false));
    txCache1.put(1L, "un");
    assertThat(txCache1.replace(1L, "uno", "eins"), is(false));
    assertThat(txCache1.replace(1L, "un", "eins"), is(true));
    assertThat(txCache1.get(1L), equalTo("eins"));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "eins");

  transactionManager.begin();
  {
    assertThat(txCache1.replace(1L, "one", "un"), is(false));
    assertThat(txCache1.replace(1L, "eins", "un"), is(true));
    assertThat(txCache1.replace(1L, "uno", "een"), is(false));
    assertThat(txCache1.get(1L), equalTo("un"));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "un");
}
 
Example #10
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void remove2ArgsAssertions(BitronixTransactionManager transactionManager, Cache<Long, String> txCache1) throws Exception {
  transactionManager.begin();
  {
    assertThat(txCache1.remove(1L, "one"), is(false));
    assertThat(txCache1.putIfAbsent(1L, "un"), is(nullValue()));
    assertThat(txCache1.remove(1L, "one"), is(false));
    assertThat(txCache1.remove(1L, "un"), is(true));
    assertThat(txCache1.remove(1L, "un"), is(false));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, null);

  transactionManager.begin();
  {
    txCache1.put(1L, "one");
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, "one");

  transactionManager.begin();
  {
    assertThat(txCache1.remove(1L, "un"), is(false));
    assertThat(txCache1.remove(1L, "one"), is(true));
    assertThat(txCache1.remove(1L, "one"), is(false));
    assertThat(txCache1.putIfAbsent(1L, "un"), is(nullValue()));
    assertThat(txCache1.remove(1L, "one"), is(false));
    assertThat(txCache1.remove(1L, "un"), is(true));
  }
  transactionManager.commit();

  assertMapping(transactionManager, txCache1, 1L, null);
}
 
Example #11
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 #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: 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 #14
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 #15
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 #16
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 #17
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void assertMapping(BitronixTransactionManager transactionManager, Cache<Long, String> cache, long key, String expected) throws Exception {
  transactionManager.begin();

  String value = cache.get(key);
  if (expected == null) {
    assertThat(value, is(nullValue()));
  } else {
    assertThat(value, equalTo(expected));
  }

  transactionManager.commit();
}
 
Example #18
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 #19
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
public TxThread(BitronixTransactionManager transactionManager, CyclicBarrier barrier) {
  this.transactionManager = transactionManager;
  this.barrier = barrier;
}
 
Example #20
Source File: CloseXADataSourceLifecycleListener.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public void setTransactionManager(BitronixTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
}
 
Example #21
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 #22
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 #23
Source File: CloseXADataSourceLifecycleListener.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public void setTransactionManager(BitronixTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
}
 
Example #24
Source File: CloseXADataSourceLifecycleListener.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public void setTransactionManager(BitronixTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
}
 
Example #25
Source File: CloseXADataSourceLifecycleListener.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public void setTransactionManager(BitronixTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
}