Java Code Examples for bitronix.tm.BitronixTransactionManager#begin()

The following examples show how to use bitronix.tm.BitronixTransactionManager#begin() . 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: 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 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: 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 12
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 13
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 14
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[]
}