Java Code Examples for javax.transaction.Status#STATUS_ACTIVE

The following examples show how to use javax.transaction.Status#STATUS_ACTIVE . 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: TransactionImpl.java    From clearpool with GNU General Public License v3.0 7 votes vote down vote up
@Override
public void registerSynchronization(Synchronization synchronization)
    throws RollbackException, SystemException {
  if (this.status != Status.STATUS_ACTIVE) {
    throw new IllegalStateException("the transaction is not active");
  }
  if (this.rollbackOnly) {
    throw new RollbackException("the transaction is signed to roll back only");
  }
  TransactionAdapter txAdapt =
      (TransactionAdapter) TransactionManagerImpl.getManager().getTransaction();
  if (txAdapt.getTx() != this) {
    throw new IllegalStateException("the transaction is not held");
  }
  if (this.synList == null) {
    this.synList = new ArrayList<Synchronization>();
  }
  this.synList.add(synchronization);
}
 
Example 2
Source File: ConcurrencyVersioningTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void endBatch(Cache<String, String> cache) {
    boolean commit = true;
    try {
        if (cache.getAdvancedCache().getTransactionManager().getStatus() == Status.STATUS_ACTIVE) {
            if (commit) {
                cache.getAdvancedCache().getTransactionManager().commit();

            } else {
                cache.getAdvancedCache().getTransactionManager().rollback();

            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: JackRabbitUserTransaction.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @see javax.transaction.UserTransaction#begin
 */
@Override
public void begin() throws NotSupportedException, SystemException {
    if (status != Status.STATUS_NO_TRANSACTION) {
        throw new IllegalStateException("Transaction already active");
    }

    try {
        xid = new XidImpl(counter++);
        xares.start(xid, XAResource.TMNOFLAGS);
        status = Status.STATUS_ACTIVE;

    } catch (XAException e) {
        final SystemException systemException = new SystemException("Unable to commit transaction: " + "XA_ERR="
                + e.errorCode);
        systemException.initCause(e);
        throw systemException;
    }
}
 
Example 4
Source File: PseudoTransactionService.java    From tomee with Apache License 2.0 6 votes vote down vote up
public void resume(final Transaction tx) throws InvalidTransactionException {
    if (tx == null) {
        throw new InvalidTransactionException("Transaction is null");
    }
    if (!(tx instanceof MyTransaction)) {
        throw new InvalidTransactionException("Unknown transaction type " + tx.getClass().getName());
    }
    final MyTransaction myTransaction = (MyTransaction) tx;

    if (threadTransaction.get() != null) {
        throw new IllegalStateException("A transaction is already active");
    }

    final int status = myTransaction.getStatus();
    if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
        throw new InvalidTransactionException("Expected transaction to be STATUS_ACTIVE or STATUS_MARKED_ROLLBACK, but was " + status);
    }

    threadTransaction.set(myTransaction);
}
 
Example 5
Source File: JackRabbitUserTransaction.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @see javax.transaction.UserTransaction#commit
 */
@Override
public void commit() throws IllegalStateException, RollbackException, SecurityException, SystemException {

    if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Transaction not active");
    }

    try {
        xares.end(xid, XAResource.TMSUCCESS);

        status = Status.STATUS_PREPARING;
        xares.prepare(xid);
        status = Status.STATUS_PREPARED;

        status = Status.STATUS_COMMITTING;
        xares.commit(xid, false);
        status = Status.STATUS_COMMITTED;

    } catch (XAException e) {

        if (e.errorCode >= XAException.XA_RBBASE && e.errorCode <= XAException.XA_RBEND) {
            RollbackException rollbackException = new RollbackException(e.toString());
            rollbackException.initCause(e);
            throw rollbackException;
        }

        final SystemException systemException = new SystemException("Unable to commit transaction: " + "XA_ERR="
                + e.errorCode);
        systemException.initCause(e);
        throw systemException;
    }
}
 
Example 6
Source File: LobCreatorUtils.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
Example 7
Source File: LobCreatorUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
Example 8
Source File: TransactionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param key The transaction key
 */
public TransactionImpl(Long key)
{
   this.key = key;
   this.status = Status.STATUS_ACTIVE;
   this.syncs = new HashSet<Synchronization>();
   this.resources = new HashMap<Object, Object>();
}
 
Example 9
Source File: UserTransactionMock.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void commit() throws
        SecurityException, IllegalStateException {
    if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Can only commit an active transaction");
    }
    status = Status.STATUS_COMMITTED;
}
 
Example 10
Source File: SpringAwareUserTransaction.java    From alfresco-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public synchronized void setRollbackOnly() throws IllegalStateException, SystemException
{
    // just a check
    TransactionInfo txnInfo = getTransactionInfo();

    int status = getStatus();
    // check the status
    if (status == Status.STATUS_MARKED_ROLLBACK)
    {
        // this is acceptable
    }
    else if (status == Status.STATUS_NO_TRANSACTION)
    {
        throw new IllegalStateException("The transaction has not been started yet");
    }
    else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK)
    {
        throw new IllegalStateException("The transaction has already been rolled back");
    }
    else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED)
    {
        throw new IllegalStateException("The transaction has already been committed");
    }
    else if (status != Status.STATUS_ACTIVE)
    {
        throw new IllegalStateException("The transaction is not active: " + status);
    }

    // mark for rollback
    txnInfo.getTransactionStatus().setRollbackOnly();
    // make sure that we record the fact that we have been marked for rollback
    internalStatus = Status.STATUS_MARKED_ROLLBACK;
    // done
    if (logger.isDebugEnabled())
    {
        logger.debug("Set transaction status to rollback only: " + this);
    }
}
 
Example 11
Source File: LobCreatorUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
Example 12
Source File: DBQueryTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@After
public void teardown() throws Exception
{
    if (txn.getStatus() == Status.STATUS_ACTIVE)
    {
        txn.rollback();
    }
    
}
 
Example 13
Source File: StatusTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static TransactionStatus translate(int status) {
	TransactionStatus transactionStatus = null;
	switch ( status ) {
		case Status.STATUS_ACTIVE:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_PREPARED:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_PREPARING:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_COMMITTING:
			transactionStatus = TransactionStatus.COMMITTING;
			break;
		case Status.STATUS_ROLLING_BACK:
			transactionStatus = TransactionStatus.ROLLING_BACK;
			break;
		case Status.STATUS_NO_TRANSACTION:
			transactionStatus = TransactionStatus.NOT_ACTIVE;
			break;
		case Status.STATUS_COMMITTED:
			transactionStatus = TransactionStatus.COMMITTED;
			break;
		case Status.STATUS_ROLLEDBACK:
			transactionStatus = TransactionStatus.ROLLED_BACK;
			break;
		case Status.STATUS_MARKED_ROLLBACK:
			transactionStatus = TransactionStatus.MARKED_ROLLBACK;
			break;
		default:
			break;
	}
	if ( transactionStatus == null ) {
		throw new TransactionException( "TransactionManager reported transaction status as unknwon" );
	}
	return transactionStatus;
}
 
Example 14
Source File: TransactionImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public synchronized void registerSynchronization(Synchronization sync)
		throws RollbackException, IllegalStateException, SystemException {

	if (this.transactionStatus == Status.STATUS_MARKED_ROLLBACK) {
		throw new RollbackException();
	} else if (this.transactionStatus == Status.STATUS_ACTIVE) {
		this.synchronizationList.registerSynchronizationQuietly(sync);
		logger.debug("{}> register-sync: sync= {}"//
				, ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), sync);
	} else {
		throw new IllegalStateException();
	}

}
 
Example 15
Source File: MockUserTransactionResolver.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public void begin() throws NotSupportedException, SystemException {
    this.begin = true;
    this.status = Status.STATUS_ACTIVE;
}
 
Example 16
Source File: JTASynchronizationService.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasTransaction() {
    return delegate.getTransactionStatus() == Status.STATUS_ACTIVE;
}
 
Example 17
Source File: SpringAwareUserTransactionTest.java    From alfresco-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void doBegin(Object arg0, TransactionDefinition arg1)
{
    status = Status.STATUS_ACTIVE;
}
 
Example 18
Source File: HomeFolderProviderSynchronizerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@After
public void tearDown() throws Exception
{
    if (trans != null)
    {
        try
        {
            trans.commit();
            trans = null;
        }
        catch (Exception e)
        {
            if ((trans.getStatus() == Status.STATUS_ACTIVE) ||
                (trans.getStatus() == Status.STATUS_MARKED_ROLLBACK))
            {
                trans.rollback();
                trans = null;
            }
        }
    }

    RetryingTransactionCallback<Void> cleanup = new RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            Set<NodeRef> adminGuestUserHomeFolders = deleteNonAdminGuestUsers();
            deleteNonAdminGuestFolders(adminGuestUserHomeFolders);
            deleteAllTenants();
            return null;
        }
    };
    try
    {
        transactionService.getRetryingTransactionHelper().doInTransaction(cleanup);
    }
    finally
    {
        AuthenticationUtil.clearCurrentSecurityContext();
        userNameMatcher.setUserNamesAreCaseSensitive(false); // Put back the default
    }
}
 
Example 19
Source File: CompensableCoordinator.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Transaction start(TransactionContext transactionContext, int flags) throws XAException {
	CompensableManager compensableManager = this.beanFactory.getCompensableManager();
	CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();
	TransactionRepository compensableRepository = this.beanFactory.getCompensableRepository();
	TransactionLock compensableLock = this.beanFactory.getCompensableLock();

	if (compensableManager.getTransactionQuietly() != null) {
		throw new XAException(XAException.XAER_PROTO);
	}

	boolean transactionContextStatefully = //
			((org.bytesoft.compensable.TransactionContext) transactionContext).isStatefully();
	if (transactionContextStatefully != this.statefully) {
		throw new XAException(XAException.XAER_PROTO);
	}

	TransactionXid globalXid = transactionContext.getXid();
	Transaction transaction = null;
	try {
		transaction = compensableRepository.getTransaction(globalXid);
	} catch (TransactionException tex) {
		throw new XAException(XAException.XAER_RMERR);
	}

	if (transaction == null) {
		transaction = new CompensableTransactionImpl((org.bytesoft.compensable.TransactionContext) transactionContext);
		((CompensableTransactionImpl) transaction).setBeanFactory(this.beanFactory);

		compensableLogger.createTransaction(((CompensableTransactionImpl) transaction).getTransactionArchive());
		compensableRepository.putTransaction(globalXid, transaction);
		logger.info("{}| compensable transaction begin!", ByteUtils.byteArrayToString(globalXid.getGlobalTransactionId()));
	} else if (transaction.getTransactionStatus() != Status.STATUS_ACTIVE) {
		throw new XAException(XAException.XAER_PROTO);
	}

	boolean locked = compensableLock.lockTransaction(globalXid, this.endpoint);
	if (locked == false) {
		throw new XAException(XAException.XAER_PROTO);
	} // end-if (locked == false)

	// if (((CompensableTransactionImpl) transaction).lock(true) == false) {
	// throw new XAException(XAException.XAER_PROTO);
	// } // end-if (available == false)

	org.bytesoft.compensable.TransactionContext compensableContext //
			= (org.bytesoft.compensable.TransactionContext) transaction.getTransactionContext();
	int propagationLevel = compensableContext.getPropagationLevel();
	compensableContext.setPropagationLevel(propagationLevel + 1);

	compensableManager.associateThread(transaction);

	return transaction;
}
 
Example 20
Source File: CacheTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testTransactionalCacheDisableSharedCaches() throws Throwable
{
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_THREE, NEW_GLOBAL_THREE, null);
    
    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();
    try
    {
        // begin a transaction
        txn.begin();
        
        // Go directly past ALL shared caches
        transactionalCache.setDisableSharedCacheReadForTransaction(true);
        
        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache", transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should not be in any cache", transactionalCache.get(UPDATE_TXN_THREE));
        
        // Update the transactional caches
        transactionalCache.put(NEW_GLOBAL_TWO, "An update");
        transactionalCache.put(UPDATE_TXN_THREE, UPDATE_TXN_THREE);
        
        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache", transactionalCache.get(NEW_GLOBAL_ONE));
        assertEquals("Value should be in transactional cache", "An update", transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("Value should be in transactional cache", UPDATE_TXN_THREE, transactionalCache.get(UPDATE_TXN_THREE));
        
        txn.commit();
        
        // Now check that values were not written through for any caches
        assertEquals("Out-of-txn read must return shared value", NEW_GLOBAL_ONE, transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should be removed from shared cache", transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("New values must be written to shared cache", UPDATE_TXN_THREE, transactionalCache.get(UPDATE_TXN_THREE));
    }
    catch (Throwable e)
    {
        if (txn.getStatus() == Status.STATUS_ACTIVE)
        {
            txn.rollback();
        }
        throw e;
    }
}