use of javax.transaction.SystemException 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.SystemException in project aries by apache.
the class Recovery method recover.
public static boolean recover(final String name, final XADataSource dataSource, final RecoverableTransactionManager transactionManager) throws IOException {
if (name != null && name.length() > 0) {
transactionManager.registerNamedXAResourceFactory(new NamedXAResourceFactory() {
public String getName() {
return name;
}
public NamedXAResource getNamedXAResource() throws SystemException {
try {
final XAConnection connection = dataSource.getXAConnection();
LOGGER.debug("new namedXAResource's connection: " + connection);
return new ConnectionAndWrapperNamedXAResource(connection.getXAResource(), getName(), connection);
} catch (Exception e) {
SystemException se = new SystemException("Failed to create ConnectionAndWrapperNamedXAResource, " + e.getLocalizedMessage());
se.initCause(e);
LOGGER.error(se.getLocalizedMessage(), se);
throw se;
}
}
public void returnNamedXAResource(NamedXAResource namedXaResource) {
if (namedXaResource instanceof ConnectionAndWrapperNamedXAResource) {
try {
LOGGER.debug("closing returned namedXAResource's connection: " + ((ConnectionAndWrapperNamedXAResource) namedXaResource).connection);
((ConnectionAndWrapperNamedXAResource) namedXaResource).connection.close();
} catch (Exception ignored) {
LOGGER.debug("failed to close returned namedXAResource: " + namedXaResource, ignored);
}
}
}
});
return true;
} else {
LOGGER.warn("Unable to recover XADataSource: aries.xa.name property not set");
return false;
}
}
use of javax.transaction.SystemException 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.SystemException in project geode by apache.
the class JCALocalTransaction method begin.
@Override
public void begin() throws ResourceException {
try {
if (!this.initDone || this.cache.isClosed()) {
this.init();
}
LogWriter logger = this.cache.getLogger();
if (logger.fineEnabled()) {
logger.fine("JCALocalTransaction::begin:");
}
TransactionManager tm = this.cache.getJTATransactionManager();
if (this.tid != null) {
throw new LocalTransactionException(" A transaction is already in progress");
}
if (tm != null && tm.getTransaction() != null) {
if (logger.fineEnabled()) {
logger.fine("JCAManagedConnection: JTA transaction is on");
}
// This is having a JTA transaction. Assuming ignore jta flag is true,
// explicitly being a gemfire transaction.
TXStateProxy tsp = this.gfTxMgr.getTXState();
if (tsp == null) {
this.gfTxMgr.begin();
tsp = this.gfTxMgr.getTXState();
tsp.setJCATransaction();
this.tid = tsp.getTransactionId();
if (logger.fineEnabled()) {
logger.fine("JCALocalTransaction:begun GFE transaction");
}
} else {
throw new LocalTransactionException("GemFire is already associated with a transaction");
}
} else {
if (logger.fineEnabled()) {
logger.fine("JCAManagedConnection: JTA Transaction does not exist.");
}
}
} catch (SystemException e) {
throw new ResourceException(e);
}
}
use of javax.transaction.SystemException in project geode by apache.
the class LocalRegion method getJTAEnlistedTX.
private TXStateInterface getJTAEnlistedTX() {
if (this.ignoreJTA) {
// fixes bug 45541
return null;
}
TXStateInterface txState = getTXState();
if (txState != null) {
return txState;
} else {
try {
if (!this.ignoreJTA && this.cache.getJTATransactionManager() != null) {
Transaction jtaTransaction = this.cache.getJTATransactionManager().getTransaction();
if (jtaTransaction == null || jtaTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
return null;
}
txState = this.cache.getTXMgr().beginJTA();
jtaTransaction.registerSynchronization(txState);
return txState;
} else {
return null;
}
} catch (SystemException se) {
// this can be thrown when the system is shutting down (see bug #39728)
this.stopper.checkCancelInProgress(se);
jtaEnlistmentFailureCleanup(txState, se);
return null;
} catch (RollbackException | IllegalStateException re) {
jtaEnlistmentFailureCleanup(txState, re);
return null;
}
}
}
Aggregations