Example usage for javax.transaction Status STATUS_MARKED_ROLLBACK

List of usage examples for javax.transaction Status STATUS_MARKED_ROLLBACK

Introduction

In this page you can find the example usage for javax.transaction Status STATUS_MARKED_ROLLBACK.

Prototype

int STATUS_MARKED_ROLLBACK

To view the source code for javax.transaction Status STATUS_MARKED_ROLLBACK.

Click Source Link

Document

A transaction is associated with the target object and it has been marked for rollback, perhaps as a result of a setRollbackOnly operation.

Usage

From source file:org.quartz.plugins.SchedulerPluginWithUserTransactionSupport.java

/**
 * If the given UserTransaction is not null, it is committed/rolledback,
 * and then returned to the UserTransactionHelper.
 *///from   w  ww. j a v  a  2 s . c om
private void resolveUserTransaction(UserTransaction userTransaction) {
    if (userTransaction != null) {
        try {
            if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                userTransaction.rollback();
            } else {
                userTransaction.commit();
            }
        } catch (Throwable t) {
            getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t);
        } finally {
            UserTransactionHelper.returnUserTransaction(userTransaction);
        }
    }
}

From source file:org.springframework.jdbc.support.lob.LobCreatorUtils.java

/**
 * 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
 *///from  w  w w  . j  a v  a  2s.c  om
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");
    }
}

From source file:org.springframework.orm.hibernate.SessionFactoryUtils.java

/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization.//from  w  w w . j ava 2  s  . com
 * @param sessionHolder the SessionHolder to check
 * @param sessionFactory the SessionFactory to get the JTA TransactionManager from
 * @param jdbcExceptionTranslator SQLExceptionTranslator to use for flushing the
 * Session on transaction synchronization (may be <code>null</code>)
 * @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 JtaSessionSynchronization(
                                    new SpringSessionSynchronization(sessionHolder, sessionFactory,
                                            jdbcExceptionTranslator, false),
                                    jtaTm));
                            sessionHolder.setSynchronizedWithTransaction(true);
                            // Switch to FlushMode.AUTO if we're not within a read-only transaction.
                            FlushMode flushMode = session.getFlushMode();
                            if (FlushMode.NEVER.equals(flushMode)) {
                                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();
    }
}

From source file:org.springframework.orm.hibernate.SessionFactoryUtils.java

/**
 * 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</code>)
 *///www .j  ava 2 s  .  c o m
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 JtaSessionSynchronization(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);
        }
    }
}

From source file:org.springframework.orm.hibernate.support.AbstractLobType.java

/**
 * This implementation delegates to nullSafeSetInternal,
 * passing in a transaction-synchronized LobCreator for the
 * LobHandler of this type./*from w  w  w .j  av  a2 s .  co  m*/
 * @see #nullSafeSetInternal
 */
public final void nullSafeSet(PreparedStatement st, Object value, int index)
        throws HibernateException, SQLException {

    if (this.lobHandler == null) {
        throw new IllegalStateException("No LobHandler found for configuration - "
                + "lobHandler property must be set on LocalSessionFactoryBean");
    }

    LobCreator lobCreator = this.lobHandler.getLobCreator();
    try {
        nullSafeSetInternal(st, index, value, lobCreator);
    } catch (IOException ex) {
        throw new HibernateException("I/O errors during LOB access", ex);
    }
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        logger.debug("Registering Spring transaction synchronization for Hibernate LOB type");
        TransactionSynchronizationManager
                .registerSynchronization(new SpringLobCreatorSynchronization(lobCreator));
    } else {
        if (this.jtaTransactionManager != null) {
            try {
                int jtaStatus = this.jtaTransactionManager.getStatus();
                if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
                    logger.debug("Registering JTA transaction synchronization for Hibernate LOB type");
                    this.jtaTransactionManager.getTransaction()
                            .registerSynchronization(new JtaLobCreatorSynchronization(lobCreator));
                    return;
                }
            } catch (Exception ex) {
                throw new DataAccessResourceFailureException(
                        "Could not register synchronization with JTA TransactionManager", ex);
            }
        }
        throw new IllegalStateException("Active Spring transaction synchronization or active "
                + "JTA transaction with 'jtaTransactionManager' on LocalSessionFactoryBean required");
    }
}

From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java

/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization./*from   w  w  w  . j  a v  a 2  s .c o  m*/
 * @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();
    }
}

From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java

/**
 * 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})
 *//*from   ww w.  j  av a2s  .c o  m*/
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);
        }
    }
}

From source file:org.topazproject.otm.impl.AbstractSession.java

private void ensureTxActive(boolean start, int txTimeout) throws OtmException {
    if (jtaTxn != null)
        return;/*  w w w. j a  va 2s.c o  m*/

    try {
        TransactionManager tm = getSessionFactory().getTransactionManager();

        jtaTxn = tm.getTransaction();
        if (jtaTxn == null) {
            if (!start)
                throw new OtmException("No active transaction");

            tm.setTransactionTimeout(txTimeout > 0 ? txTimeout : 0);
            tm.begin();
            jtaTxn = tm.getTransaction();
        }

        jtaTxn.registerSynchronization(new Synchronization() {
            public void beforeCompletion() {
                if (getFlushMode().implies(FlushMode.commit) && !isRollback())
                    flush();
            }

            public void afterCompletion(int status) {
                endTransaction();
            }

            private boolean isRollback() throws OtmException {
                try {
                    return (jtaTxn.getStatus() == Status.STATUS_MARKED_ROLLBACK);
                } catch (Exception e) {
                    throw new OtmException("Error getting rollback-only status", e);
                }
            }
        });
    } catch (OtmException oe) {
        throw oe;
    } catch (Exception e) {
        throw new OtmException("Error setting up transaction", e);
    }
}

From source file:org.topazproject.otm.impl.TransactionImpl.java

public boolean isRollbackOnly() throws OtmException {
    try {/*from  ww w . ja  v a2 s . c o m*/
        return (jtaTxn.getStatus() == Status.STATUS_MARKED_ROLLBACK);
    } catch (Exception e) {
        throw new OtmException("Error getting rollback-only status", e);
    }
}