Search in sources :

Example 1 with TransactionNotActiveException

use of org.datanucleus.exceptions.TransactionNotActiveException in project datanucleus-core by datanucleus.

the class TransactionImpl method rollback.

/**
 * Method to rollback the transaction.
 */
public void rollback() {
    if (!isActive()) {
        throw new TransactionNotActiveException();
    }
    long startTime = System.currentTimeMillis();
    try {
        // whether the transaction can be completed
        boolean canComplete = true;
        committing = true;
        try {
            // TODO Is this really needed? om.preRollback does all necessary
            flush();
        } finally {
            // even if flush fails, we ignore and go ahead cleaning up and rolling back everything ahead...
            try {
                internalPreRollback();
            } catch (NucleusUserException e) {
                // catch only NucleusUserException; they must be cascade up to user code and transaction is still alive
                if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
                    NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
                }
                canComplete = false;
                throw e;
            } finally {
                if (canComplete) {
                    try {
                        internalRollback();
                    } finally {
                        try {
                            active = false;
                            if (ec.getStatistics() != null) {
                                ec.getStatistics().transactionRolledBack(System.currentTimeMillis() - beginTime);
                            }
                        } finally {
                            listenersPerTransaction.clear();
                            // Reset rollbackOnly flag
                            rollbackOnly = false;
                            if (sync != null) {
                                sync.afterCompletion(Status.STATUS_ROLLEDBACK);
                            }
                        }
                    }
                }
            }
        }
    } catch (NucleusUserException e) {
        throw e;
    } catch (NucleusException e) {
        throw new NucleusDataStoreException(Localiser.msg("015009"), e);
    } finally {
        committing = false;
    }
    if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
        NucleusLogger.TRANSACTION.debug(Localiser.msg("015023", System.currentTimeMillis() - startTime));
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 2 with TransactionNotActiveException

use of org.datanucleus.exceptions.TransactionNotActiveException in project datanucleus-core by datanucleus.

the class TransactionImpl method commit.

/**
 * Method to commit the transaction.
 */
public void commit() {
    if (!isActive()) {
        throw new TransactionNotActiveException();
    }
    // the exception and call rollback themselves. i.e we don't need to close the DB connection or set "active" to false.
    if (rollbackOnly) {
        // Throw an exception since can only exit via rollback
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(Localiser.msg("015020"));
        }
        throw new NucleusDataStoreException(Localiser.msg("015020")).setFatal();
    }
    long startTime = System.currentTimeMillis();
    boolean success = false;
    // whether the transaction can be completed
    boolean canComplete = true;
    List<Throwable> errors = new ArrayList();
    try {
        // TODO Is this needed? om.preCommit will handle flush calls
        flush();
        internalPreCommit();
        internalCommit();
        success = true;
    } catch (RollbackException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (HeuristicRollbackException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (HeuristicMixedException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (NucleusUserException e) {
        // they must be cascade up to user code and transaction is still alive
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        canComplete = false;
        throw e;
    } catch (NucleusException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } finally {
        if (canComplete) {
            try {
                if (!success) {
                    rollback();
                } else {
                    internalPostCommit();
                }
            } catch (Throwable e) {
                errors.add(e);
            }
        }
    }
    if (!errors.isEmpty()) {
        throw new NucleusTransactionException(Localiser.msg("015007"), errors.toArray(new Throwable[errors.size()]));
    }
    if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
        NucleusLogger.TRANSACTION.debug(Localiser.msg("015022", System.currentTimeMillis() - startTime));
    }
}
Also used : NucleusTransactionException(org.datanucleus.transaction.NucleusTransactionException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) HeuristicRollbackException(org.datanucleus.transaction.HeuristicRollbackException) RollbackException(org.datanucleus.transaction.RollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) HeuristicRollbackException(org.datanucleus.transaction.HeuristicRollbackException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) HeuristicMixedException(org.datanucleus.transaction.HeuristicMixedException) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 TransactionNotActiveException (org.datanucleus.exceptions.TransactionNotActiveException)2 ArrayList (java.util.ArrayList)1 HeuristicMixedException (org.datanucleus.transaction.HeuristicMixedException)1 HeuristicRollbackException (org.datanucleus.transaction.HeuristicRollbackException)1 NucleusTransactionException (org.datanucleus.transaction.NucleusTransactionException)1 RollbackException (org.datanucleus.transaction.RollbackException)1