Java Code Examples for org.ehcache.transactions.xa.txmgr.btm.BitronixTransactionManagerLookup

Following code examples demonstrate how to use org.ehcache.transactions.xa.txmgr.btm.BitronixTransactionManagerLookupfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.transactions.xa.txmgr.btm.BitronixTransactionManagerLookupand various code implementation of this class.

  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()).add(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();
    }
  } 


  public void testClusteredCacheWithXA() throws Exception {
    TransactionManagerServices.getConfiguration().setJournal("null");

    BitronixTransactionManager transactionManager =
        TransactionManagerServices.getTransactionManager();

    PersistentCacheManager persistentCacheManager = null;
    try {
      CacheManagerBuilder.newCacheManagerBuilder()
          .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
          .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB))
              )
                  .add(new XAStoreConfiguration("xaCache"))
                  .build()
          )
          .build(true);
    } catch (StateTransitionException e) {
      assertThat(e.getCause().getCause().getMessage(), is("Unsupported resource type : interface org.ehcache.clustered.client.config.DedicatedClusteredResourcePool"));
    }

    transactionManager.shutdown();
  } 

  public void testAtomicsWithoutLoaderWriter() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                    .heap(10, EntryUnit.ENTRIES)
                    .offheap(10, MemoryUnit.MB)
                )
        .withExpiry(Expirations.timeToLiveExpiration(new Duration(1, TimeUnit.SECONDS)));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

    putIfAbsentAssertions(transactionManager, txCache1);
    txCache1.clear();
    remove2ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();
    replace2ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();
    replace3ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testCopiers() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    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 cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .with(new CacheManagerPersistenceConfiguration(new File(getStoragePath())))
        .withCache("txCache1", cacheConfigurationBuilder
                .add(new XAStoreConfiguration("txCache1"))
                .add(new DefaultCopierConfiguration<>(LongCopier.class, DefaultCopierConfiguration.Type.KEY))
                .add(new DefaultCopierConfiguration<>(StringCopier.class, DefaultCopierConfiguration.Type.VALUE))
                .build()
        )
        .withCache("txCache2", cacheConfigurationBuilder
            .add(new XAStoreConfiguration("txCache2"))
            .add(new DefaultCopierConfiguration<>(LongCopier.class, DefaultCopierConfiguration.Type.KEY))
            .add(new DefaultCopierConfiguration<>(StringCopier.class, DefaultCopierConfiguration.Type.VALUE))
            .build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

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


    transactionManager.begin();
    {
      txCache1.put(1L, "eins");
      txCache2.put(1L, "uno");
    }
    transactionManager.commit();


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


    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testConcurrentTx() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                        .heap(10, EntryUnit.ENTRIES)
                        .offheap(10, MemoryUnit.MB))
        .withExpiry(Expirations.timeToLiveExpiration(new Duration(1, TimeUnit.SECONDS)));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .withCache("txCache2", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache2")).build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

    CyclicBarrier barrier = new CyclicBarrier(2);

    TxThread tx1 = new TxThread(transactionManager, barrier) {
      @Override
      public void doTxWork() throws Exception {
        Thread.currentThread().setName("tx1");
        txCache1.put(0L, "zero");
        txCache1.put(1L, "one");
        txCache2.put(-1L, "-one");
        txCache2.put(-2L, "-two");
      }
    };
    TxThread tx2 = new TxThread(transactionManager, barrier) {
      @Override
      public void doTxWork() throws Exception {
        Thread.currentThread().setName("tx2");
        txCache1.put(1L, "un");
        txCache1.put(2L, "deux");
        txCache2.put(-1L, "-un");
        txCache2.put(-3L, "-trois");
      }
    };
    tx1.start();
    tx2.start();
    tx1.join();
    tx2.join();

    if (tx1.ex != null) {
      System.err.println("tx1 error");
      tx1.ex.printStackTrace();
    }
    if (tx2.ex != null) {
      System.err.println("tx2 error");
      tx2.ex.printStackTrace();
    }

    transactionManager.begin();
    assertThat(txCache1.get(0L), equalTo("zero"));
    assertThat(txCache1.get(1L), is(nullValue()));
    assertThat(txCache1.get(2L), equalTo("deux"));
    assertThat(txCache2.get(-1L), is(nullValue()));
    assertThat(txCache2.get(-2L), equalTo("-two"));
    assertThat(txCache2.get(-3L), equalTo("-trois"));
    transactionManager.commit();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testEndToEnd() throws Exception {
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                .heap(10, EntryUnit.ENTRIES)
                .offheap(10, MemoryUnit.MB));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .withCache("txCache2", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache2")).build())
        .withCache("nonTxCache", cacheConfigurationBuilder.build())
        .build(true);

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

    nonTxCache.put(1L, "eins");
    assertThat(nonTxCache.get(1L), equalTo("eins"));

    try {
      txCache1.put(1L, "one");
      fail("expected XACacheException");
    } catch (XACacheException e) {
    }

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

    transactionManager.begin();
    {
      txCache1.get(1L);
      txCache2.get(1L);
    }
    transactionManager.commit();

    transactionManager.begin();
    {
      String s = txCache1.get(1L);
      assertThat(s, equalTo("one"));
      txCache1.remove(1L);

      Transaction suspended = transactionManager.suspend();
      transactionManager.begin();
      {
        txCache2.put(1L, "uno");
        String s2 = txCache1.get(1L);
        assertThat(s2, equalTo("one"));
      }
      transactionManager.commit();
      transactionManager.resume(suspended);

      String s1 = txCache2.get(1L);
      assertThat(s1, equalTo("uno"));

    }
    transactionManager.commit();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testAtomicsWithLoaderWriter() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();
    SampleLoaderWriter<Long, String> loaderWriter = new SampleLoaderWriter<>();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                    .heap(10, EntryUnit.ENTRIES)
                    .offheap(10, MemoryUnit.MB))
        .withExpiry(Expirations.timeToLiveExpiration(new Duration(1, TimeUnit.SECONDS)))
        .withLoaderWriter(loaderWriter);

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

    putIfAbsentAssertions(transactionManager, txCache1);
    txCache1.clear();
    loaderWriter.clear();
    remove2ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();
    loaderWriter.clear();
    replace2ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();
    loaderWriter.clear();
    replace3ArgsAssertions(transactionManager, txCache1);
    txCache1.clear();
    loaderWriter.clear();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testExpiry() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                        .heap(10, EntryUnit.ENTRIES)
                        .offheap(10, MemoryUnit.MB))
        .withExpiry(Expirations.timeToLiveExpiration(new Duration(1, TimeUnit.SECONDS)));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .withCache("txCache2", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache2")).build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

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


    transactionManager.begin();
    {
      txCache1.put(1L, "eins");
      txCache2.put(1L, "uno");
    }
    transactionManager.commit();

    testTimeSource.advanceTime(2000);

    transactionManager.begin();
    {
      assertThat(txCache1.get(1L), is(nullValue()));
      assertThat(txCache2.get(1L), is(nullValue()));
    }
    transactionManager.commit();


    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testTimeout() throws Exception {
    TestTimeSource testTimeSource = new TestTimeSource();
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                .heap(10, EntryUnit.ENTRIES)
                .offheap(10, MemoryUnit.MB));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .with(new CacheManagerPersistenceConfiguration(new File(getStoragePath())))
        .withCache("txCache1", cacheConfigurationBuilder
                .add(new XAStoreConfiguration("txCache1"))
                .add(new DefaultCopierConfiguration<>(LongCopier.class, DefaultCopierConfiguration.Type.KEY))
                .add(new DefaultCopierConfiguration<>(StringCopier.class, DefaultCopierConfiguration.Type.VALUE))
                .build()
        )
        .withCache("txCache2", cacheConfigurationBuilder
            .add(new XAStoreConfiguration("txCache2"))
            .add(new DefaultCopierConfiguration<>(LongCopier.class, DefaultCopierConfiguration.Type.KEY))
            .add(new DefaultCopierConfiguration<>(StringCopier.class, DefaultCopierConfiguration.Type.VALUE))
            .build())
        .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

    transactionManager.setTransactionTimeout(1);
    transactionManager.begin();
    {
      txCache1.put(1L, "one");
      txCache2.put(1L, "un");
      testTimeSource.advanceTime(2000);
    }
    try {
      transactionManager.commit();
      fail("Expected RollbackException");
    } catch (RollbackException e) {
    }

    transactionManager.setTransactionTimeout(1);
    transactionManager.begin();
    {
      txCache1.put(1L, "one");
      txCache2.put(1L, "un");
      testTimeSource.advanceTime(2000);
      try {
        txCache2.put(1L, "uno");
        fail("expected XACacheException");
      } catch (XACacheException e) {
      }
    }
    try {
      transactionManager.commit();
      fail("Expected RollbackException");
    } catch (RollbackException e) {
    }

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testRecoveryWithInflightTx() throws Exception {
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder()
                .heap(10, EntryUnit.ENTRIES)
                .offheap(10, MemoryUnit.MB));

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .withCache("txCache2", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache2")).build())
        .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
        .build(true);

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

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


    transactionManager.begin();
    {
      txCache1.remove(1L);
      txCache2.remove(1L);
    }
    transactionManager.getCurrentTransaction().addTransactionStatusChangeListener((oldStatus, newStatus) -> {
      if (newStatus == Status.STATUS_PREPARED) {
        Recoverer recoverer = TransactionManagerServices.getRecoverer();
        recoverer.run();
        assertThat(recoverer.getCommittedCount(), is(0));
        assertThat(recoverer.getRolledbackCount(), is(0));
      }
    });
    transactionManager.commit();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testRecoveryAfterCrash() throws Exception {
    BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();

    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 cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .with(new CacheManagerPersistenceConfiguration(new File(getStoragePath())))
        .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
        .withCache("txCache2", cacheConfigurationBuilder.add(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) {
    }

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

    setUpBtmConfig();
    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();

    cacheManager.close();
    transactionManager.shutdown();
  } 

  public void testTranslateServiceCreationConfiguration() {
    TxCacheManagerServiceConfigurationParser configTranslator = new TxCacheManagerServiceConfigurationParser();
    LookupTransactionManagerProviderConfiguration lookupTransactionManagerProviderConfiguration =
      new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class);

    Node retElement = configTranslator.unparseServiceCreationConfiguration(lookupTransactionManagerProviderConfiguration);
    String inputString = "<tx:jta-tm " +
                         "transaction-manager-lookup-class = \"org.ehcache.transactions.xa.txmgr.btm.BitronixTransactionManagerLookup\" " +
    assertElement(inputString, retElement);
  } 

    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))
          .add(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();
    } 

Advertisement
Javadoc
@author Ludovic Orban
Advertisement