Example #1
0
  private XATransactionContext getTransactionContext() {
    try {
      Transaction transaction = getCurrentTransaction();
      EhcacheXAResourceImpl xaResource =
          (EhcacheXAResourceImpl) transactionToXAResourceMap.get(transaction);
      if (xaResource == null) {
        return null;
      }
      XATransactionContext transactionContext = xaResource.getCurrentTransactionContext();

      if (transactionContext == null) {
        transactionManagerLookup.register(xaResource, false);
        LOG.debug("creating new XA context");
        transactionContext = xaResource.createTransactionContext();
        xaResource.addTwoPcExecutionListener(new UnregisterXAResource());
      } else {
        transactionContext = xaResource.getCurrentTransactionContext();
      }

      LOG.debug("using XA context {}", transactionContext);
      return transactionContext;
    } catch (SystemException e) {
      throw new TransactionException("cannot get the current transaction", e);
    } catch (RollbackException e) {
      throw new TransactionException("transaction rolled back", e);
    }
  }
Example #2
0
  /**
   * Constructor
   *
   * @param transactionManagerLookup the transaction manager lookup implementation
   * @param softLockManager the soft lock manager
   * @param transactionIdFactory the transaction ID factory
   * @param cache the cache
   * @param store the underlying store
   * @param copyStrategy the original copy strategy
   */
  public XATransactionStore(
      TransactionManagerLookup transactionManagerLookup,
      SoftLockManager softLockManager,
      TransactionIDFactory transactionIdFactory,
      Ehcache cache,
      Store store,
      ReadWriteCopyStrategy<Element> copyStrategy) {
    super(store, copyStrategy);
    this.transactionManagerLookup = transactionManagerLookup;
    this.transactionIdFactory = transactionIdFactory;
    if (transactionManagerLookup.getTransactionManager() == null) {
      throw new TransactionException(
          "no JTA transaction manager could be located, cannot bind twopc cache with JTA");
    }
    this.softLockManager = softLockManager;
    this.cache = cache;

    // this xaresource is for initial registration and recovery
    this.recoveryResource =
        new EhcacheXAResourceImpl(
            cache,
            underlyingStore,
            transactionManagerLookup,
            softLockManager,
            transactionIdFactory,
            copyStrategy,
            commitObserver,
            rollbackObserver,
            recoveryObserver);
    transactionManagerLookup.register(recoveryResource, true);
  }