use of javax.transaction.Synchronization in project tomee by apache.
the class TransactionalTest method checked.
@Test
public void checked() throws Exception {
for (int i = 0; i < 2; i++) {
final AtomicInteger status = new AtomicInteger();
try {
bean.checked(new Runnable() {
@Override
public void run() {
try {
OpenEJB.getTransactionManager().getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// no-op
}
@Override
public void afterCompletion(int state) {
status.set(state);
}
});
} catch (final RollbackException | SystemException e) {
fail();
}
}
});
fail();
} catch (final AnCheckedException e) {
// no-op
}
assertEquals(Status.STATUS_COMMITTED, status.get());
}
}
use of javax.transaction.Synchronization in project tomee by apache.
the class TransactionalTest method rollbackException.
@Test
public void rollbackException() throws Exception {
for (int i = 0; i < 2; i++) {
final AtomicInteger status = new AtomicInteger();
final TransactionManager transactionManager = OpenEJB.getTransactionManager();
transactionManager.begin();
transactionManager.getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// no-op
}
@Override
public void afterCompletion(int state) {
status.set(state);
}
});
try {
bean.anException();
fail();
} catch (final AnException e) {
// no-op
}
OpenEJB.getTransactionManager().rollback();
assertEquals(Status.STATUS_ROLLEDBACK, status.get());
}
}
use of javax.transaction.Synchronization in project tomee by apache.
the class AutoConnectionTracker method handleObtained.
/**
* Proxies new connection handles so we can detect when they have been garbage collected.
*
* @param interceptor the interceptor used to release the managed connection when the handled is garbage collected.
* @param connectionInfo the connection that was obtained
* @param reassociate should always be false
*/
@SuppressWarnings("unchecked")
@Override
public void handleObtained(final ConnectionTrackingInterceptor interceptor, final ConnectionInfo connectionInfo, final boolean reassociate) throws ResourceException {
if (txMgr != null && registry != null) {
try {
final TransactionImpl currentTx = (TransactionImpl) txMgr.getTransaction();
if (currentTx != null) {
Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>> txConnections = (Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>>) registry.getResource(KEY);
if (txConnections == null) {
txConnections = new HashMap<ManagedConnectionInfo, Map<ConnectionInfo, Object>>();
registry.putResource(KEY, txConnections);
}
Map<ConnectionInfo, Object> connectionObjects = txConnections.get(connectionInfo.getManagedConnectionInfo());
if (connectionObjects == null) {
connectionObjects = new HashMap<ConnectionInfo, Object>();
txConnections.put(connectionInfo.getManagedConnectionInfo(), connectionObjects);
}
connectionObjects.put(connectionInfo, connectionInfo.getConnectionProxy());
registry.registerInterposedSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(final int status) {
final Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>> txConnections = (Map<ManagedConnectionInfo, Map<ConnectionInfo, Object>>) currentTx.getResource(KEY);
if (txConnections != null && txConnections.size() > 0) {
for (final ManagedConnectionInfo managedConnectionInfo : txConnections.keySet()) {
final StringBuilder sb = new StringBuilder();
final Collection<ConnectionInfo> connectionInfos = txConnections.get(managedConnectionInfo).keySet();
for (final ConnectionInfo connectionInfo : connectionInfos) {
sb.append("\n ").append("Connection handle opened at ").append(stackTraceToString(connectionInfo.getTrace().getStackTrace()));
}
logger.warning("Transaction complete, but connection still has handles associated: " + managedConnectionInfo + "\nAbandoned connection information: " + sb.toString());
}
}
}
});
}
} catch (SystemException | ClassCastException e) {
// ignore
}
}
if (!reassociate) {
proxyConnection(interceptor, connectionInfo);
}
}
use of javax.transaction.Synchronization in project Payara by payara.
the class JavaEETransactionImpl method commit.
@Override
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
checkTransationActive();
// If this transaction is set for timeout, cancel it as it is in the commit state
if (isTimerTask) {
cancelTimerTask();
}
// END local transaction timeout
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "--In JavaEETransactionImpl.commit, jtsTx=" + jtsTx + " nonXAResource=" + nonXAResource);
}
commitStarted = true;
boolean success = false;
if (jtsTx != null) {
try {
jtsTx.commit();
success = true;
} catch (HeuristicMixedException e) {
success = true;
throw e;
} finally {
((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
onTxCompletion(success);
try {
localTxStatus = jtsTx.getStatus();
} catch (Exception e) {
localTxStatus = Status.STATUS_NO_TRANSACTION;
}
jtsTx = null;
}
} else {
// local tx
Exception caughtException = null;
try {
if (timedOut) {
// rollback nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
throw new RollbackException(sm.getString("enterprise_distributedtx.rollback_timeout"));
}
if (isRollbackOnly()) {
// rollback nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
throw new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
}
// call beforeCompletion
for (int i = 0; i < syncs.size(); i++) {
try {
Synchronization sync = (Synchronization) syncs.elementAt(i);
sync.beforeCompletion();
} catch (RuntimeException ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
setRollbackOnly();
caughtException = ex;
break;
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
// XXX-V2 no setRollbackOnly() ???
}
}
for (int i = 0; i < interposedSyncs.size(); i++) {
try {
Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
sync.beforeCompletion();
} catch (RuntimeException ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
setRollbackOnly();
caughtException = ex;
break;
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
// XXX-V2 no setRollbackOnly() ???
}
}
// calls marked it for rollback.
if (isRollbackOnly()) {
// Check if it is a Local Transaction
RollbackException rbe = null;
if (jtsTx == null) {
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
// else it is a global transaction
} else {
jtsTx.rollback();
localTxStatus = Status.STATUS_ROLLEDBACK;
rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
}
// RollbackException doesn't have a constructor that takes a Throwable.
if (caughtException != null) {
rbe.initCause(caughtException);
}
throw rbe;
}
// beforeCompletions registered the first XA resource.
if (jtsTx != null) {
jtsTx.commit();
// Note: JTS will not call afterCompletions in this case,
// because no syncs have been registered with JTS.
// So afterCompletions are called in finally block below.
} else {
// do single-phase commit on nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().commit(xid, true);
}
}
// V2-XXX should this be STATUS_NO_TRANSACTION ?
localTxStatus = Status.STATUS_COMMITTED;
success = true;
} catch (RollbackException ex) {
// V2-XXX is this correct ?
localTxStatus = Status.STATUS_ROLLEDBACK;
throw ex;
} catch (SystemException ex) {
// localTxStatus = Status.STATUS_ROLLEDBACK; // V2-XXX is this correct ?
localTxStatus = Status.STATUS_COMMITTING;
success = true;
throw ex;
} catch (Exception ex) {
// V2-XXX is this correct ?
localTxStatus = Status.STATUS_ROLLEDBACK;
SystemException exc = new SystemException();
exc.initCause(ex);
throw exc;
} finally {
((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
for (int i = 0; i < interposedSyncs.size(); i++) {
try {
Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
sync.afterCompletion(localTxStatus);
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
}
}
// call afterCompletions
for (int i = 0; i < syncs.size(); i++) {
try {
Synchronization sync = (Synchronization) syncs.elementAt(i);
sync.afterCompletion(localTxStatus);
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
}
}
onTxCompletion(success);
jtsTx = null;
}
}
}
use of javax.transaction.Synchronization in project Payara by payara.
the class SynchronizationImpl method before_completion.
public void before_completion() {
// Regular syncs first then the interposed syncs
Enumeration e = syncs.elements();
while (e.hasMoreElements()) {
Synchronization sync = (Synchronization) e.nextElement();
try {
sync.beforeCompletion();
} catch (RuntimeException rex) {
try {
state.setRollbackOnly();
} catch (Exception ex1) {
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex1);
}
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", rex);
throw rex;
} catch (Exception ex) {
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex);
}
}
Enumeration e1 = interposedSyncs.elements();
while (e1.hasMoreElements()) {
Synchronization sync = (Synchronization) e1.nextElement();
try {
sync.beforeCompletion();
} catch (RuntimeException rex) {
try {
state.setRollbackOnly();
} catch (Exception ex1) {
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex1);
}
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", rex);
throw rex;
} catch (Exception ex) {
_logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex);
}
}
state.beforeCompletion();
}
Aggregations