use of javax.transaction.RollbackException in project aries by apache.
the class TxDBServlet method insertIntoTransaction.
/**
* This method demonstrates how to enlist JDBC connection into Transaction according OSGi enterprise specification.
*
* @param xads XADataSource
* @param tm TransactionManager
* @param value which will be inserted into table
* @param toCommit Specify if the transaction will be committed or rolledback
* @throws SQLException
* @throws GenericJTAException
*/
private void insertIntoTransaction(XADataSource xads, TransactionManager tm, String value, boolean toCommit) throws SQLException, GenericJTAException {
XAConnection xaConnection = xads.getXAConnection();
Connection connection = xaConnection.getConnection();
XAResource xaResource = xaConnection.getXAResource();
try {
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
PreparedStatement insertStatement = connection.prepareStatement(INSERT_INTO_TABLE);
insertStatement.setString(1, value);
insertStatement.executeUpdate();
if (toCommit) {
transaction.commit();
} else {
transaction.rollback();
}
} catch (RollbackException e) {
throw new GenericJTAException(e);
} catch (SecurityException e) {
throw new GenericJTAException(e);
} catch (IllegalStateException e) {
throw new GenericJTAException(e);
} catch (HeuristicMixedException e) {
throw new GenericJTAException(e);
} catch (HeuristicRollbackException e) {
throw new GenericJTAException(e);
} catch (SystemException e) {
throw new GenericJTAException(e);
} catch (NotSupportedException e) {
throw new GenericJTAException(e);
}
}
use of javax.transaction.RollbackException in project aries by apache.
the class TxInterceptorImpl method postCallWithReturn.
@Override
public void postCallWithReturn(ComponentMetadata cm, Method m, Object returnType, Object preCallToken) throws Exception {
LOGGER.debug("PostCallWithReturn for bean {}, method {}.", getCmId(cm), m);
// it is possible transaction is not involved at all
if (preCallToken == null) {
return;
}
if (!(preCallToken instanceof TransactionToken)) {
throw new IllegalStateException("Expected a TransactionToken from preCall but got " + preCallToken);
}
final TransactionToken token = (TransactionToken) preCallToken;
safeEndCoordination(token);
try {
token.getTransactionAttribute().finish(tm, token);
} catch (Exception e) {
// We are throwing an exception, so we don't error it out
LOGGER.debug("Exception while completing transaction.", e);
RollbackException rbe = new javax.transaction.RollbackException();
rbe.addSuppressed(e);
throw rbe;
}
}
use of javax.transaction.RollbackException in project aries by apache.
the class AbstractIntegrationTest method assertInsertWithRuntimeExceptionRolledBack.
// Test with client transaction and runtime exception - the user transaction is rolled back
protected void assertInsertWithRuntimeExceptionRolledBack() throws Exception {
TestBean bean = getBean();
int initialRows = counter.countRows();
if (clientTransaction) {
tran.begin();
}
bean.insertRow("testWithClientTranAndWithRuntimeException", 1, null);
try {
bean.insertRow("testWithClientTranAndWithRuntimeException", 2, new RuntimeException("Dummy exception"));
} catch (RuntimeException e) {
Assert.assertEquals("Dummy exception", e.getMessage());
}
if (clientTransaction) {
try {
tran.commit();
fail("RollbackException not thrown");
} catch (RollbackException e) {
// Ignore expected
}
}
int finalRows = counter.countRows();
// In case of client transaction both are rolled back
// In case of container transaction only second insert is rolled back
assertEquals("Added rows", clientTransaction ? 0 : 1, finalRows - initialRows);
}
use of javax.transaction.RollbackException in project aries by apache.
the class XaConnectionPool method createSession.
@Override
public Session createSession(boolean transacted, int ackMode) throws JMSException {
try {
boolean isXa = (transactionManager != null && transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION);
if (isXa) {
// if the xa tx aborts inflight we don't want to auto create a
// local transaction or auto ack
transacted = false;
ackMode = Session.CLIENT_ACKNOWLEDGE;
} else if (transactionManager != null) {
// cmt or transactionManager managed
transacted = false;
if (ackMode == Session.SESSION_TRANSACTED) {
ackMode = Session.AUTO_ACKNOWLEDGE;
}
}
PooledSession session = (PooledSession) super.createSession(transacted, ackMode);
if (isXa) {
session.addSessionEventListener(new PooledSessionEventListener() {
@Override
public void onTemporaryQueueCreate(TemporaryQueue tempQueue) {
}
@Override
public void onTemporaryTopicCreate(TemporaryTopic tempTopic) {
}
@Override
public void onSessionClosed(PooledSession session) {
session.setIgnoreClose(true);
session.setIsXa(false);
}
});
session.setIgnoreClose(true);
session.setIsXa(true);
transactionManager.getTransaction().registerSynchronization(new Synchronization(session));
incrementReferenceCount();
transactionManager.getTransaction().enlistResource(createXaResource(session));
} else {
session.setIgnoreClose(false);
}
return session;
} catch (RollbackException e) {
final JMSException jmsException = new JMSException("Rollback Exception");
jmsException.initCause(e);
throw jmsException;
} catch (SystemException e) {
final JMSException jmsException = new JMSException("System Exception");
jmsException.initCause(e);
throw jmsException;
}
}
use of javax.transaction.RollbackException in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrowWithJTA.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testAfterCommitFailedOnTransactionWriterThrowWithJTA() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
UserTransaction userTx = (UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
userTx.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
userTx.commit();
fail("Commit should have thrown RollbackException");
} catch (RollbackException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() + " is not a SynchronizationCommitConflictException", expected.getCause() instanceof SynchronizationCommitConflictException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(1, this.afterRollbacks);
}
Aggregations