Search in sources :

Example 1 with RollbackException

use of javax.transaction.RollbackException in project hibernate-orm by hibernate.

the class BeforeCompletionFailureTest method testUniqueConstraintViolationDuringManagedFlush.

@Test
@TestForIssue(jiraKey = "HHH-9888")
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // set up test data
    Session session = openSession();
    session.getTransaction().begin();
    session.save(newEntity(1));
    session.getTransaction().commit();
    session.close();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // do the test
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction ("CMT" style)
    tm.begin();
    session = openSession();
    session.save(newEntity(2));
    // which should lead to the UK violation
    try {
        tm.commit();
        fail("Expecting a failure from JTA commit");
    } catch (RollbackException expected) {
        log.info("Test encountered expected JTA RollbackException; looking for nested JDBCException", expected);
        boolean violationExceptionFound = false;
        Throwable cause = expected;
        while (cause != null) {
            if (cause instanceof JDBCException) {
                log.info("Found JDBCException, assuming related to UK violation", cause);
                violationExceptionFound = true;
                break;
            }
            cause = cause.getCause();
        }
        if (!violationExceptionFound) {
            fail("Did not find JDBCException in JTA RollbackException chain");
        }
    } finally {
        if (!((SessionImplementor) session).isClosed()) {
            session.close();
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // clean up test data
    session = openSession();
    session.getTransaction().begin();
    session.createQuery("delete SimpleEntity").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : JDBCException(org.hibernate.JDBCException) TransactionManager(javax.transaction.TransactionManager) RollbackException(javax.transaction.RollbackException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with RollbackException

use of javax.transaction.RollbackException in project neo4j-mobile-android by neo4j-contrib.

the class TransactionImpl method enlistResource.

public synchronized boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException, SystemException {
    if (xaRes == null) {
        throw new IllegalArgumentException("Null xa resource");
    }
    if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
        try {
            if (resourceList.size() == 0) {
                if (!globalStartRecordWritten) {
                    txManager.writeStartRecord(globalId);
                    globalStartRecordWritten = true;
                }
                //
                byte[] branchId = txManager.getBranchId(xaRes);
                Xid xid = new XidImpl(globalId, branchId);
                resourceList.add(new ResourceElement(xid, xaRes));
                xaRes.start(xid, XAResource.TMNOFLAGS);
                try {
                    txManager.getTxLog().addBranch(globalId, branchId);
                } catch (IOException e) {
                    log.log(Level.SEVERE, "Error writing transaction log", e);
                    txManager.setTmNotOk(e);
                    throw Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e);
                }
                return true;
            }
            Xid sameRmXid = null;
            Iterator<ResourceElement> itr = resourceList.iterator();
            while (itr.hasNext()) {
                ResourceElement re = itr.next();
                if (sameRmXid == null && re.getResource().isSameRM(xaRes)) {
                    sameRmXid = re.getXid();
                }
                if (xaRes == re.getResource()) {
                    if (re.getStatus() == RS_SUSPENDED) {
                        xaRes.start(re.getXid(), XAResource.TMRESUME);
                    } else {
                        // either enlisted or delisted
                        // is TMJOIN correct then?
                        xaRes.start(re.getXid(), XAResource.TMJOIN);
                    }
                    re.setStatus(RS_ENLISTED);
                    return true;
                }
            }
            if (// should we join?
            sameRmXid != null) {
                addResourceToList(sameRmXid, xaRes);
                xaRes.start(sameRmXid, XAResource.TMJOIN);
            } else // new branch
            {
                // ResourceElement re = resourceList.getFirst();
                byte[] branchId = txManager.getBranchId(xaRes);
                Xid xid = new XidImpl(globalId, branchId);
                addResourceToList(xid, xaRes);
                xaRes.start(xid, XAResource.TMNOFLAGS);
                try {
                    txManager.getTxLog().addBranch(globalId, branchId);
                } catch (IOException e) {
                    log.log(Level.SEVERE, "Error writing transaction log", e);
                    txManager.setTmNotOk(e);
                    throw Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e);
                }
            }
            return true;
        } catch (XAException e) {
            log.log(Level.SEVERE, "Unable to enlist resource[" + xaRes + "]", e);
            status = Status.STATUS_MARKED_ROLLBACK;
            return false;
        }
    } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK || status == Status.STATUS_MARKED_ROLLBACK) {
        throw new RollbackException("Tx status is: " + txManager.getTxStatusAsString(status));
    }
    throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) IOException(java.io.IOException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 3 with RollbackException

use of javax.transaction.RollbackException in project neo4j-mobile-android by neo4j-contrib.

the class TxManager method rollbackCommit.

private void rollbackCommit(Thread thread, TransactionImpl tx) throws HeuristicMixedException, RollbackException, SystemException {
    try {
        tx.doRollback();
    } catch (XAException e) {
        log.log(Level.SEVERE, "Unable to rollback marked transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e);
        setTmNotOk(e);
        throw logAndReturn("TM error tx rollback commit", Exceptions.withCause(new HeuristicMixedException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode), e));
    }
    tx.doAfterCompletion();
    txThreadMap.remove(thread);
    try {
        if (tx.isGlobalStartRecordWritten()) {
            getTxLog().txDone(tx.getGlobalId());
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error writing transaction log", e);
        setTmNotOk(e);
        throw logAndReturn("TM error tx rollback commit", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
    }
    tx.setStatus(Status.STATUS_NO_TRANSACTION);
    throw new RollbackException("Failed to commit, transaction rolledback");
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) IOException(java.io.IOException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException)

Example 4 with RollbackException

use of javax.transaction.RollbackException in project requery by requery.

the class ManagedTransaction method commit.

@Override
public void commit() {
    if (initiatedTransaction) {
        try {
            transactionListener.beforeCommit(entities.types());
            getUserTransaction().commit();
            transactionListener.afterCommit(entities.types());
        } catch (RollbackException | SystemException | HeuristicMixedException | HeuristicRollbackException e) {
            throw new TransactionException(e);
        }
    }
    try {
        entities.clear();
    } finally {
        close();
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 5 with RollbackException

use of javax.transaction.RollbackException in project spring-framework by spring-projects.

the class JtaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction();
    try {
        int jtaStatus = txObject.getUserTransaction().getStatus();
        if (jtaStatus == Status.STATUS_NO_TRANSACTION) {
            // In any case, the transaction is already fully cleaned up.
            throw new UnexpectedRollbackException("JTA transaction already completed - probably rolled back");
        }
        if (jtaStatus == Status.STATUS_ROLLEDBACK) {
            // IllegalStateException expected on JBoss; call still necessary.
            try {
                txObject.getUserTransaction().rollback();
            } catch (IllegalStateException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Rollback failure with transaction already marked as rolled back: " + ex);
                }
            }
            throw new UnexpectedRollbackException("JTA transaction already rolled back (probably due to a timeout)");
        }
        txObject.getUserTransaction().commit();
    } catch (RollbackException ex) {
        throw new UnexpectedRollbackException("JTA transaction unexpectedly rolled back (maybe due to a timeout)", ex);
    } catch (HeuristicMixedException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_MIXED, ex);
    } catch (HeuristicRollbackException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_ROLLED_BACK, ex);
    } catch (IllegalStateException ex) {
        throw new TransactionSystemException("Unexpected internal transaction state", ex);
    } catch (SystemException ex) {
        throw new TransactionSystemException("JTA failure on commit", ex);
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4