Java Code Examples for javax.transaction.Transaction#getStatus()

The following examples show how to use javax.transaction.Transaction#getStatus() . 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: TransactionManagerImpl.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit() throws RollbackException,
                            HeuristicMixedException,
                            HeuristicRollbackException,
                            SecurityException,
                            IllegalStateException,
                            SystemException
{
   Transaction tx = registry.getTransaction();

   if (tx == null)
      throw new SystemException();

   if (tx.getStatus() == Status.STATUS_ROLLEDBACK ||
       tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
      throw new RollbackException();

   registry.commitTransaction();
}
 
Example 2
Source File: TransactionContext.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * The transaction scoped context is active when a transaction is active.
 */
@Override
public boolean isActive() {
    Transaction transaction = getCurrentTransaction();
    if (transaction == null) {
        return false;
    }

    try {
        int currentStatus = transaction.getStatus();
        return currentStatus == Status.STATUS_ACTIVE ||
                currentStatus == Status.STATUS_MARKED_ROLLBACK ||
                currentStatus == Status.STATUS_PREPARED ||
                currentStatus == Status.STATUS_UNKNOWN ||
                currentStatus == Status.STATUS_PREPARING ||
                currentStatus == Status.STATUS_COMMITTING ||
                currentStatus == Status.STATUS_ROLLING_BACK;
    } catch (SystemException e) {
        throw new RuntimeException("Error getting the status of the current transaction", e);
    }
}
 
Example 3
Source File: TxUtils.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Is the transaction uncommitted
 * @param tx The transaction
 * @return True if uncommitted; otherwise false
 */
public static boolean isUncommitted(Transaction tx)
{
   if (tx == null)
      return false;
   
   try
   {
      int status = tx.getStatus();

      return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
   }
   catch (SystemException error)
   {
      throw new RuntimeException("Error during isUncommitted()", error);
   }
}
 
Example 4
Source File: TimerData.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void registerTimerDataSynchronization() throws TimerStoreException {
    if (synchronizationRegistered) {
        return;
    }

    try {
        final Transaction transaction = timerService.getTransactionManager().getTransaction();
        final int status = transaction == null ? Status.STATUS_NO_TRANSACTION : transaction.getStatus();

        if (transaction != null && status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
            transaction.registerSynchronization(new TimerDataSynchronization());
            synchronizationRegistered = true;
            return;
        }
    } catch (final Exception e) {
        log.warning("Unable to register timer data transaction synchronization", e);
    }

    // there either wasn't a transaction or registration failed... call transactionComplete directly
    transactionComplete(true);
}
 
Example 5
Source File: GlobalTransactionTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void testSetRollbackOnly() {
  try {
    utx.begin();
    utx.setRollbackOnly();
    Transaction txn = tm.getTransaction();
    if (txn.getStatus() != Status.STATUS_MARKED_ROLLBACK) {
      utx.rollback();
      fail("testSetRollbackonly failed");
    }
    utx.rollback();
  }
  catch (Exception e) {
    fail("exception in testSetRollbackonly due to " + e);
    e.printStackTrace();
  }
}
 
Example 6
Source File: TransactionManagerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit() throws RollbackException,
                            HeuristicMixedException,
                            HeuristicRollbackException,
                            SecurityException,
                            IllegalStateException,
                            SystemException
{
   Transaction tx = registry.getTransaction();

   if (tx == null)
      throw new SystemException();

   if (tx.getStatus() == Status.STATUS_ROLLEDBACK ||
       tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
      throw new RollbackException();

   registry.commitTransaction();
}
 
Example 7
Source File: GlobalTransactionTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void testSetRollbackOnly() {
  try {
    utx.begin();
    utx.setRollbackOnly();
    Transaction txn = tm.getTransaction();
    if (txn.getStatus() != Status.STATUS_MARKED_ROLLBACK) {
      utx.rollback();
      fail("testSetRollbackonly failed");
    }
    utx.rollback();
  }
  catch (Exception e) {
    fail("exception in testSetRollbackonly due to " + e);
    e.printStackTrace();
  }
}
 
Example 8
Source File: UserTransactionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit() throws RollbackException,
                            HeuristicMixedException,
                            HeuristicRollbackException,
                            SecurityException,
                            IllegalStateException,
                            SystemException
{
   Transaction tx = registry.getTransaction();

   if (tx == null)
      throw new SystemException();

   if (tx.getStatus() == Status.STATUS_ROLLING_BACK ||
       tx.getStatus() == Status.STATUS_ROLLEDBACK ||
       tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
      throw new RollbackException();

   registry.commitTransaction();
}
 
Example 9
Source File: TxUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Is the transaction active
 * @param tx The transaction
 * @return True if active; otherwise false
 */
public static boolean isActive(Transaction tx)
{
   if (tx == null)
      return false;
   
   try
   {
      int status = tx.getStatus();

      return status == Status.STATUS_ACTIVE;
   }
   catch (SystemException error)
   {
      throw new RuntimeException("Error during isActive()", error);
   }
}
 
Example 10
Source File: ReplicatedServer.java    From unitime with Apache License 2.0 5 votes vote down vote up
private boolean inTransaction() {
	try {
		Transaction tx = getTransactionManager().getTransaction();
		return tx != null && tx.getStatus() == Status.STATUS_ACTIVE;
	} catch (SystemException e) {
		return false;
	}
}
 
Example 11
Source File: InterceptorBase.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
public boolean isTransactionActive(final Transaction current) {
    if (current == null) {
        return false;
    }

    try {
        final int status = current.getStatus();
        return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
    } catch (final SystemException e) {
        return false;
    }
}
 
Example 12
Source File: JtaTransactionPolicy.java    From tomee with Apache License 2.0 5 votes vote down vote up
public boolean isTransactionActive() {
    final Transaction trasaction = getCurrentTransaction();
    if (trasaction == null) {
        return false;
    }

    try {
        final int status = trasaction.getStatus();
        return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
    } catch (final javax.transaction.SystemException e) {
        return false;
    }
}
 
Example 13
Source File: JtaTransactionPolicy.java    From tomee with Apache License 2.0 5 votes vote down vote up
public boolean isRollbackOnly() {
    final Transaction trasaction = getCurrentTransaction();
    if (trasaction != null) {
        try {
            final int status = trasaction.getStatus();
            return status == Status.STATUS_MARKED_ROLLBACK;
        } catch (final javax.transaction.SystemException e) {
            return false;
        }
    } else {
        return rollbackOnly;
    }
}
 
Example 14
Source File: TransactionManagerImpl.java    From clearpool with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getStatus() throws SystemException {
  Transaction action = TX_HOLDER.get();
  if (action == null) {
    return Status.STATUS_NO_TRANSACTION;
  }
  return action.getStatus();
}
 
Example 15
Source File: TransactionContext.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * True if the transaction is active or marked for rollback only.
 *
 * @return true if the transaction is active or marked for rollback only; false otherwise
 * @throws SQLException
 *             if a problem occurs obtaining the transaction status
 */
public boolean isActive() throws SQLException {
    try {
        final Transaction transaction = this.transactionRef.get();
        if (transaction == null) {
            return false;
        }
        final int status = transaction.getStatus();
        return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
    } catch (final SystemException e) {
        throw new SQLException("Unable to get transaction status", e);
    }
}
 
Example 16
Source File: SimpleTransactionSynchronizationRegistry.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Transaction getActiveTransaction() {
    try {
        final Transaction transaction = transactionManager.getTransaction();
        if (transaction == null) {
            throw new IllegalStateException("No transaction active");
        }
        final int status = transaction.getStatus();
        if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
            throw new IllegalStateException("No transaction active");
        }
        return transaction;
    } catch (final SystemException e) {
        throw new IllegalStateException("No transaction active", e);
    }
}
 
Example 17
Source File: TransactionManagerImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int getStatus() throws SystemException
{
   Transaction tx = registry.getTransaction();

   if (tx == null)
      return Status.STATUS_NO_TRANSACTION;

   return tx.getStatus();
}
 
Example 18
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization.
 * @param sessionHolder the SessionHolder to check
 * @param sessionFactory the SessionFactory to get the JTA TransactionManager from
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 * @return the associated Session, if any
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 */
private static Session getJtaSynchronizedSession(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		// Check whether JTA transaction management is active ->
		// fetch pre-bound Session for the current JTA transaction, if any.
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction)
		try {
			// Look for transaction-specific Session.
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					Session session = sessionHolder.getValidatedSession(jtaTx);
					if (session == null && !sessionHolder.isSynchronizedWithTransaction()) {
						// No transaction-specific Session found: If not already marked as
						// synchronized with transaction, register the default thread-bound
						// Session as JTA-transactional. If there is no default Session,
						// we're a new inner JTA transaction with an outer one being suspended:
						// In that case, we'll return null to trigger opening of a new Session.
						session = sessionHolder.getValidatedSession();
						if (session != null) {
							logger.debug("Registering JTA transaction synchronization for existing Hibernate Session");
							sessionHolder.addSession(jtaTx, session);
							jtaTx.registerSynchronization(
									new SpringJtaSynchronizationAdapter(
											new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false),
											jtaTm));
							sessionHolder.setSynchronizedWithTransaction(true);
							// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
							// with FlushMode.NEVER, which needs to allow flushing within the transaction.
							FlushMode flushMode = session.getFlushMode();
							if (flushMode.lessThan(FlushMode.COMMIT)) {
								session.setFlushMode(FlushMode.AUTO);
								sessionHolder.setPreviousFlushMode(flushMode);
							}
						}
					}
					return session;
				}
			}
			// No transaction active -> simply return default thread-bound Session, if any
			// (possibly from OpenSessionInViewFilter/Interceptor).
			return sessionHolder.getValidatedSession();
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException("Could not check JTA transaction", ex);
		}
	}
	else {
		// No JTA TransactionManager -> simply return default thread-bound Session, if any
		// (possibly from OpenSessionInViewFilter/Interceptor).
		return sessionHolder.getValidatedSession();
	}
}
 
Example 19
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Register a JTA synchronization for the given Session, if any.
 * @param sessionHolder the existing thread-bound SessionHolder, if any
 * @param session the Session to register
 * @param sessionFactory the SessionFactory that the Session was created with
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 */
private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session);
	if (jtaTm != null) {
		try {
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for new Hibernate Session");
					SessionHolder holderToUse = sessionHolder;
					// Register JTA Transaction with existing SessionHolder.
					// Create a new SessionHolder if none existed before.
					if (holderToUse == null) {
						holderToUse = new SessionHolder(jtaTx, session);
					}
					else {
						holderToUse.addSession(jtaTx, session);
					}
					jtaTx.registerSynchronization(
							new SpringJtaSynchronizationAdapter(
									new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true),
									jtaTm));
					holderToUse.setSynchronizedWithTransaction(true);
					if (holderToUse != sessionHolder) {
						TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
					}
				}
			}
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException(
					"Could not register synchronization with JTA TransactionManager", ex);
		}
	}
}
 
Example 20
Source File: SessionFactoryUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Register a JTA synchronization for the given Session, if any.
 * @param sessionHolder the existing thread-bound SessionHolder, if any
 * @param session the Session to register
 * @param sessionFactory the SessionFactory that the Session was created with
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 */
private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session);
	if (jtaTm != null) {
		try {
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for new Hibernate Session");
					SessionHolder holderToUse = sessionHolder;
					// Register JTA Transaction with existing SessionHolder.
					// Create a new SessionHolder if none existed before.
					if (holderToUse == null) {
						holderToUse = new SessionHolder(jtaTx, session);
					}
					else {
						holderToUse.addSession(jtaTx, session);
					}
					jtaTx.registerSynchronization(
							new SpringJtaSynchronizationAdapter(
									new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true),
									jtaTm));
					holderToUse.setSynchronizedWithTransaction(true);
					if (holderToUse != sessionHolder) {
						TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
					}
				}
			}
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException(
					"Could not register synchronization with JTA TransactionManager", ex);
		}
	}
}