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();
}
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));
}
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");
}
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();
}
}
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);
}
}
Aggregations