use of javax.transaction.HeuristicRollbackException in project geode by apache.
the class TransactionManagerImpl method commit.
/**
* Complete the transaction associated with the current thread by calling the
* GlobalTransaction.commit(). When this method completes, the thread is no longer associated with
* a transaction.
*
* @throws RollbackException - Thrown to indicate that the transaction has been rolled back rather
* than committed.
* @throws HeuristicMixedException - Thrown to indicate that a heuristic decision was made and
* that some relevant updates have been committed while others have been rolled back.
* @throws HeuristicRollbackException - Thrown to indicate that a heuristic decision was made and
* that all relevant updates have been rolled back.
* @throws java.lang.SecurityException - Thrown to indicate that the thread is not allowed to
* commit the transaction.
* @throws java.lang.IllegalStateException - Thrown if the current thread is not associated with a
* transaction.
* @throws SystemException - Thrown if the transaction manager encounters an unexpected error
* condition.
*
* @see javax.transaction.TransactionManager#commit()
*/
public void commit() throws HeuristicRollbackException, RollbackException, HeuristicMixedException, SystemException {
if (!isActive) {
throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
}
int cozOfException = -1;
Transaction transactionImpl = getTransaction();
if (transactionImpl == null) {
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_TRANSACTION.toLocalizedString();
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception);
throw new IllegalStateException(exception);
}
GlobalTransaction gtx = getGlobalTransaction(transactionImpl);
if (gtx == null) {
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_GLOBAL_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_GLOBAL_TRANSACTION.toLocalizedString();
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception);
throw new SystemException(exception);
}
boolean isCommit = false;
// ensure only one thread can commit. Use a synchronized block
// Asif
int status = -1;
if (((status = gtx.getStatus()) == Status.STATUS_ACTIVE) || status == Status.STATUS_MARKED_ROLLBACK) {
synchronized (gtx) {
if ((status = gtx.getStatus()) == Status.STATUS_ACTIVE) {
gtx.setStatus(Status.STATUS_COMMITTING);
isCommit = true;
} else if (status == Status.STATUS_MARKED_ROLLBACK) {
gtx.setStatus(Status.STATUS_ROLLING_BACK);
cozOfException = MARKED_ROLLBACK;
} else {
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_NOT_ACTIVE_CANNOT_BE_COMMITTED_TRANSACTION_STATUS_0.toLocalizedString(Integer.valueOf(status));
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception);
throw new IllegalStateException(exception);
}
}
} else {
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NOT_ACTIVE_AND_CANNOT_BE_COMMITTED.toLocalizedString();
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception);
throw new IllegalStateException(exception);
}
// Only one thread can call commit (the first thread to do reach the block
// above).
// Before commiting the notifications to be done before the done are called
// the global transaction is called and then the after completion
// notifications
// are taken care of. The transactions associated to the global
// transactions are
// removed from the map and also the tread to transaction.
//
// Asif : Store the thrown Exception in case of commit .
// Reuse it for thrwing later.
// Asif TODO:Verify if it is a good practise
boolean isClean = false;
Exception e = null;
try {
((TransactionImpl) transactionImpl).notifyBeforeCompletion();
isClean = true;
} catch (Exception ge) {
// Asif : Just mark the Tranxn to setRollbackOnly to ensure Rollback
setRollbackOnly();
cozOfException = EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION;
e = ge;
}
// will be harmless
if (isCommit) {
synchronized (gtx) {
if ((status = gtx.getStatus()) == Status.STATUS_COMMITTING) {
// and appropriately mark the exception code
try {
gtx.commit();
} catch (RollbackException rbe) {
e = rbe;
cozOfException = COMMIT_FAILED_SO_ROLLEDBAK;
} catch (SystemException se) {
e = se;
cozOfException = COMMIT_FAILED_ROLLBAK_ALSO_FAILED;
}
} else if (status == Status.STATUS_ROLLING_BACK) {
try {
gtx.rollback();
if (isClean)
cozOfException = MARKED_ROLLBACK;
} catch (SystemException se) {
e = se;
cozOfException = ROLLBAK_FAILED;
}
}
}
} else {
try {
gtx.rollback();
} catch (SystemException se) {
e = se;
cozOfException = ROLLBAK_FAILED;
}
}
try {
((TransactionImpl) transactionImpl).notifyAfterCompletion(status = gtx.getStatus());
} catch (Exception ge) {
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (writer.infoEnabled())
writer.info(LocalizedStrings.TransactionManagerImpl_EXCEPTION_IN_NOTIFY_AFTER_COMPLETION_DUE_TO__0, ge.getMessage(), ge);
}
Thread thread = Thread.currentThread();
transactionMap.remove(thread);
this.gtxSet.remove(gtx);
if (status != Status.STATUS_COMMITTED) {
switch(cozOfException) {
case EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION:
{
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_OF_EXCEPTION_IN_NOTIFYBEFORECOMPLETION_FUNCTION_CALL_ACTUAL_EXCEPTION_0.toLocalizedString();
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception, e);
RollbackException re = new RollbackException(exception);
re.initCause(e);
throw re;
}
case MARKED_ROLLBACK:
{
String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_A_USER_MARKED_IT_FOR_ROLLBACK.toLocalizedString();
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(exception, e);
throw new RollbackException(exception);
}
case COMMIT_FAILED_SO_ROLLEDBAK:
{
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(e);
throw (RollbackException) e;
}
case COMMIT_FAILED_ROLLBAK_ALSO_FAILED:
case ROLLBAK_FAILED:
{
LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
if (VERBOSE)
writer.fine(e);
throw (SystemException) e;
}
}
}
gtx.setStatus(Status.STATUS_NO_TRANSACTION);
}
use of javax.transaction.HeuristicRollbackException in project jackrabbit by apache.
the class UserTransactionImpl method commit.
/**
* @see javax.transaction.UserTransaction#commit
*/
public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
if (status != Status.STATUS_ACTIVE) {
throw new IllegalStateException("Transaction not active");
}
try {
for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
XAResource resource = (XAResource) it.next();
XidImpl xid = (XidImpl) xaResources.get(resource);
resource.end(xid, XAResource.TMSUCCESS);
}
status = Status.STATUS_PREPARING;
for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
XAResource resource = (XAResource) it.next();
XidImpl xid = (XidImpl) xaResources.get(resource);
resource.prepare(xid);
}
status = Status.STATUS_PREPARED;
status = Status.STATUS_COMMITTING;
if (distributedThreadAccess) {
Thread distributedThread = new Thread() {
public void run() {
try {
for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
XAResource resource = (XAResource) it.next();
XidImpl xid = (XidImpl) xaResources.get(resource);
resource.commit(xid, false);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
};
distributedThread.start();
distributedThread.join(1000);
if (distributedThread.isAlive()) {
throw new SystemException("Commit from different thread but same XID must not block");
}
} else {
for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
XAResource resource = (XAResource) it.next();
XidImpl xid = (XidImpl) xaResources.get(resource);
resource.commit(xid, false);
}
}
status = Status.STATUS_COMMITTED;
} catch (XAException e) {
if (e.errorCode >= XAException.XA_RBBASE && e.errorCode <= XAException.XA_RBEND) {
RollbackException re = new RollbackException("Transaction rolled back: XA_ERR=" + e.errorCode);
re.initCause(e.getCause());
throw re;
} else {
SystemException se = new SystemException("Unable to commit transaction: XA_ERR=" + e.errorCode);
se.initCause(e.getCause());
throw se;
}
} catch (InterruptedException e) {
throw new SystemException("Thread.join() interrupted");
}
}
use of javax.transaction.HeuristicRollbackException in project wildfly by wildfly.
the class MessageEndpointInvocationHandler method afterDelivery.
@Override
public void afterDelivery() throws ResourceException {
final TransactionManager tm = getTransactionManager();
try {
if (currentTx != null) {
if (currentTx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
tm.rollback();
else
tm.commit();
currentTx = null;
}
if (previousTx != null) {
tm.resume(previousTx);
previousTx = null;
}
} catch (InvalidTransactionException e) {
throw new LocalTransactionException(e);
} catch (HeuristicMixedException e) {
throw new LocalTransactionException(e);
} catch (SystemException e) {
throw new LocalTransactionException(e);
} catch (HeuristicRollbackException e) {
throw new LocalTransactionException(e);
} catch (RollbackException e) {
throw new LocalTransactionException(e);
} finally {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader);
previousClassLoader = null;
}
}
use of javax.transaction.HeuristicRollbackException in project wildfly by wildfly.
the class DatabaseTimerPersistence method shouldRun.
@Override
public boolean shouldRun(TimerImpl timer, TransactionManager tm) {
if (!allowExecution) {
//timers never execute on this node
return false;
}
String loadTimer = sql(UPDATE_RUNNING);
Connection connection = null;
PreparedStatement statement = null;
try {
try {
connection = dataSource.getConnection();
statement = connection.prepareStatement(loadTimer);
statement.setString(1, TimerState.IN_TIMEOUT.name());
setNodeName(TimerState.IN_TIMEOUT, statement, 2);
statement.setString(3, timer.getId());
statement.setString(4, TimerState.IN_TIMEOUT.name());
statement.setString(5, TimerState.RETRY_TIMEOUT.name());
if (timer.getNextExpiration() == null) {
statement.setTimestamp(6, null);
} else {
statement.setTimestamp(6, timestamp(timer.getNextExpiration()));
}
} catch (SQLException e) {
// something wrong with the preparation
throw new RuntimeException(e);
}
tm.begin();
int affected = statement.executeUpdate();
tm.commit();
return affected == 1;
} catch (SQLException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
// failed to update the DB
try {
tm.rollback();
} catch (IllegalStateException | SecurityException | SystemException rbe) {
EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(rbe);
}
EjbLogger.EJB3_TIMER_LOGGER.debugf(e, "Timer %s not running due to exception ", timer);
return false;
} catch (NotSupportedException e) {
// happen from tm.begin, no rollback necessary
EjbLogger.EJB3_TIMER_LOGGER.timerNotRunning(e, timer);
return false;
} finally {
safeClose(statement);
safeClose(connection);
}
}
use of javax.transaction.HeuristicRollbackException in project wildfly by wildfly.
the class BeforeCompletionExceptionDestroysBeanTestCase method testExceptionInBeforeCompletionDestroysBean.
@Test
public void testExceptionInBeforeCompletionDestroysBean() throws NamingException, SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException {
final UserTransaction userTransaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
final BeforeCompletionSFSB bean = (BeforeCompletionSFSB) new InitialContext().lookup("java:module/" + BeforeCompletionSFSB.class.getSimpleName());
//begin a transaction
userTransaction.begin();
bean.enlist();
//commit, this should destroy the bean, as it's @BeforeCompletion method will throw an exception
try {
userTransaction.commit();
} catch (RollbackException expected) {
}
try {
userTransaction.begin();
bean.enlist();
throw new RuntimeException("Expected SFSB to be destroyed");
} catch (NoSuchEJBException expected) {
} finally {
userTransaction.rollback();
}
}
Aggregations