Example usage for org.hibernate TransactionException TransactionException

List of usage examples for org.hibernate TransactionException TransactionException

Introduction

In this page you can find the example usage for org.hibernate TransactionException TransactionException.

Prototype

public TransactionException(String message) 

Source Link

Document

Constructs a TransactionException using the specified information.

Usage

From source file:com.cubeia.firebase.service.persistence.jpa.SystemJTATransaction.java

License:Open Source License

public void begin() throws HibernateException {
    if (begun) {/*from  w  w  w. j ava 2  s .  c o m*/
        return;
    }
    if (commitFailed) {
        throw new TransactionException("cannot re-start transaction after failed commit");
    }

    log.debug("begin");

    try {
        newTransaction = ut.getStatus() == Status.STATUS_NO_TRANSACTION;
        if (newTransaction) {
            ut.begin();
            log.debug("Began a new JTA transaction");
        }
    } catch (Exception e) {
        log.error("JTA transaction begin failed", e);
        throw new TransactionException("JTA transaction begin failed", e);
    }

    /*if (newTransaction) {
       // don't need a synchronization since we are committing
       // or rolling back the transaction ourselves - assuming
       // that we do no work in beforeTransactionCompletion()
       synchronization = false;
    }*/

    boolean synchronization = jdbcContext.registerSynchronizationIfPossible();

    if (!newTransaction && !synchronization) {
        log.warn("You should set hibernate.transaction.manager_lookup_class if cache is enabled");
    }

    if (!synchronization) {
        //if we could not register a synchronization,
        //do the before/after completion callbacks
        //ourself (but we need to let jdbcContext
        //know that this is what we are going to
        //do, so it doesn't keep trying to register
        //synchronizations)
        callback = jdbcContext.registerCallbackIfNecessary();
    }

    begun = true;
    commitSucceeded = false;

    jdbcContext.afterTransactionBegin(this);
}

From source file:com.cubeia.firebase.service.persistence.jpa.SystemJTATransaction.java

License:Open Source License

public void commit() throws HibernateException {
    if (!begun)//  w w  w .  j  av  a2 s  .  co  m
        throw new TransactionException("Transaction not successfully started");

    log.debug("commit");

    boolean flush = !transactionContext.isFlushModeNever()
            && (callback || !transactionContext.isFlushBeforeCompletionEnabled());

    if (flush) {
        transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
    }
    if (callback && newTransaction) {
        jdbcContext.beforeTransactionCompletion(this);
    }
    closeIfRequired();
    if (newTransaction) {
        try {
            ut.commit();
            commitSucceeded = true;
            log.debug("Committed JTA UserTransaction");
        } catch (Exception e) {
            commitFailed = true; // so the transaction is already rolled back, by JTA spec
            log.error("JTA commit failed", e);
            throw new TransactionException("JTA commit failed: ", e);
        } finally {
            afterCommitRollback();
        }
    } else {
        // this one only really needed for badly-behaved applications!
        // (if the TransactionManager has a Sychronization registered,
        // its a noop)
        // (actually we do need it for downgrading locks)
        afterCommitRollback();
    }

}

From source file:com.cubeia.firebase.service.persistence.jpa.SystemJTATransaction.java

License:Open Source License

public void rollback() throws HibernateException {
    if (!begun && !commitFailed) {
        throw new TransactionException("Transaction not successfully started");
    }/*from  ww w . j  ava2s .c  o  m*/
    log.debug("rollback");
    try {
        closeIfRequired();
    } catch (Exception e) {
        log.error("could not close session during rollback", e);
    }
    try {
        if (newTransaction) {
            if (!commitFailed) {
                ut.rollback();
                log.debug("Rolled back JTA UserTransaction");
            }
        } else {
            ut.setRollbackOnly();
            log.debug("set JTA UserTransaction to rollback only");
        }
    } catch (Exception e) {
        log.error("JTA rollback failed", e);
        throw new TransactionException("JTA rollback failed", e);
    } finally {
        afterCommitRollback();
    }
}

From source file:com.redhat.rhn.common.hibernate.NestedTransactionFactory.java

License:Open Source License

/**
 * {@inheritDoc}//from   w w  w. ja  v  a  2  s.  c o  m
 */
public Transaction createTransaction(JDBCContext jdbcContext, Context context) throws HibernateException {
    ToplevelTransaction txn = getTransaction();
    if (txn == null || jdbcContext != txn.jdbcCtx) {
        // Create new toplevel (JDBC) txn
        Transaction realTxn = jdbcTxnFactory.createTransaction(jdbcContext, context);
        txn = new ToplevelTransaction(realTxn, jdbcContext, context);
        TXN_TLS.set(txn);
        return txn;
    } else {
        // An outermost transaction exists
        throw new TransactionException("Nesting transactions is not allowed.");
    }
}

From source file:org.apache.ode.daohib.JotmTransaction.java

License:Open Source License

/**
 * {@inheritDoc}//from w w  w  . ja va  2  s .c o m
 */
public void begin() throws HibernateException {
    if (begun) {
        return;
    }
    if (commitFailed) {
        throw new TransactionException("cannot re-start transaction after failed commit");
    }

    log.debug("begin");

    try {
        newTransaction = userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION;
        if (newTransaction) {
            userTransaction.begin();
            log.debug("Began a new JTA transaction");
        }
    } catch (Exception e) {
        log.error("JTA transaction begin failed", e);
        throw new TransactionException("JTA transaction begin failed", e);
    }

    /*if (newTransaction) {
    // don't need a synchronization since we are committing
    // or rolling back the transaction ourselves - assuming
    // that we do no work in beforeTransactionCompletion()
    synchronization = false;
    }*/

    boolean synchronization = jdbcContext.registerSynchronizationIfPossible();

    if (!newTransaction && !synchronization) {
        log.warn("You should set hibernate.transaction.manager_lookup_class if cache is enabled");
    }

    if (!synchronization) {
        //if we could not register a synchronization,
        //do the before/after completion callbacks
        //ourself (but we need to let jdbcContext
        //know that this is what we are going to
        //do, so it doesn't keep trying to register
        //synchronizations)
        callback = jdbcContext.registerCallbackIfNecessary();
    }

    begun = true;
    commitSucceeded = false;

    jdbcContext.afterTransactionBegin(this);
}

From source file:org.apache.ode.daohib.JotmTransaction.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w w w  .java2  s .co  m*/
 */
public void commit() throws HibernateException {
    if (!begun) {
        throw new TransactionException("Transaction not successfully started");
    }

    log.debug("commit");

    boolean flush = !transactionContext.isFlushModeNever()
            && (callback || !transactionContext.isFlushBeforeCompletionEnabled());

    if (flush) {
        transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
    }

    if (callback && newTransaction) {
        jdbcContext.beforeTransactionCompletion(this);
    }

    closeIfRequired();

    if (newTransaction) {
        try {
            userTransaction.commit();
            commitSucceeded = true;
            log.debug("Committed JTA UserTransaction");
        } catch (Exception e) {
            commitFailed = true; // so the transaction is already rolled back, by JTA spec
            log.error("JTA commit failed", e);
            throw new TransactionException("JTA commit failed: ", e);
        } finally {
            afterCommitRollback();
        }
    } else {
        // this one only really needed for badly-behaved applications!
        // (if the TransactionManager has a Sychronization registered,
        // its a noop)
        // (actually we do need it for downgrading locks)
        afterCommitRollback();
    }

}

From source file:org.apache.ode.daohib.JotmTransaction.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w  w  w.  j  av  a  2 s.co  m*/
 */
public void rollback() throws HibernateException {
    if (!begun && !commitFailed) {
        throw new TransactionException("Transaction not successfully started");
    }

    log.debug("rollback");

    try {
        closeIfRequired();
    } catch (Exception e) {
        // swallow it, and continue to roll back JTA transaction
        log.error("could not close session during rollback", e);
    }

    try {
        if (newTransaction) {
            if (!commitFailed) {
                userTransaction.rollback();
                log.debug("Rolled back JTA UserTransaction");
            }
        } else {
            userTransaction.setRollbackOnly();
            log.debug("set JTA UserTransaction to rollback only");
        }
    } catch (Exception e) {
        log.error("JTA rollback failed", e);
        throw new TransactionException("JTA rollback failed", e);
    } finally {
        afterCommitRollback();
    }
}

From source file:org.apache.ode.daohib.JotmTransactionFactory.java

License:Open Source License

/**
 * Get the {@link UserTransaction} reference.
 *
 * @return The appropriate {@link UserTransaction} reference.
 *//*from   www.  j a v a 2  s. co m*/
protected UserTransaction getUserTransaction() {
    final String utName = getUserTransactionName();
    log.debug("Attempting to locate UserTransaction via JNDI [" + utName + "]");

    try {
        UserTransaction ut = (UserTransaction) getInitialContext().lookup(utName);
        if (ut == null) {
            throw new TransactionException(
                    "Naming service lookup for UserTransaction returned null [" + utName + "]");
        }

        log.trace("Obtained UserTransaction");

        return ut;
    } catch (NamingException ne) {
        throw new TransactionException("Could not find UserTransaction in JNDI [" + utName + "]", ne);
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.DualNodeJtaPlatformImpl.java

License:LGPL

@Override
public UserTransaction retrieveUserTransaction() {
    throw new TransactionException("UserTransaction not used in these tests");
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.DualNodeJtaPlatformImpl.java

License:LGPL

@Override
public void registerSynchronization(Synchronization synchronization) {
    try {//  w  w  w .  j av  a  2 s.  co  m
        retrieveTransactionManager().getTransaction().registerSynchronization(synchronization);
    } catch (Exception e) {
        throw new TransactionException("Could not obtain transaction from TM");
    }
}