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 TransactionalTest method classLevel.
@Test
public void classLevel() throws Exception {
for (int i = 0; i < 2; i++) {
final AtomicInteger status = new AtomicInteger();
try {
bean.classLevel(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 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 hibernate-orm by hibernate.
the class XaTransactionImpl method rollback.
public void rollback() throws IllegalStateException, SystemException {
status = Status.STATUS_ROLLING_BACK;
runXaResourceRollback();
status = Status.STATUS_ROLLEDBACK;
if (connection != null) {
try {
connection.rollback();
connection.close();
} catch (SQLException sqle) {
status = Status.STATUS_UNKNOWN;
throw new SystemException();
}
}
if (synchronizations != null) {
for (int i = 0; i < synchronizations.size(); i++) {
Synchronization s = (Synchronization) synchronizations.get(i);
if (s != null)
s.afterCompletion(status);
}
}
// status = Status.STATUS_NO_TRANSACTION;
jtaTransactionManager.endCurrent(this);
}
use of javax.transaction.Synchronization in project hibernate-orm by hibernate.
the class VersionedTest method testStaleRead.
protected ByRef<Object> testStaleRead(BiConsumer<Session, Item> consumer) throws Exception {
AtomicReference<Exception> synchronizationException = new AtomicReference<>();
CountDownLatch syncLatch = new CountDownLatch(1);
CountDownLatch commitLatch = new CountDownLatch(1);
Future<Boolean> action = executor.submit(() -> withTxSessionApply(s -> {
try {
((SharedSessionContractImplementor) s).getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(int i) {
syncLatch.countDown();
try {
awaitOrThrow(commitLatch);
} catch (Exception e) {
synchronizationException.set(e);
}
}
});
Item item = s.load(Item.class, itemId);
consumer.accept(s, item);
s.flush();
} catch (StaleStateException e) {
log.info("Exception thrown: ", e);
markRollbackOnly(s);
return false;
} catch (PessimisticLockException e) {
log.info("Exception thrown: ", e);
markRollbackOnly(s);
return false;
}
return true;
}));
awaitOrThrow(syncLatch);
ByRef<Object> entryRef = new ByRef<>(null);
try {
withTxSession(s -> {
Item item = s.load(Item.class, itemId);
assertEquals("Original item", item.getDescription());
entryRef.set(assertSingleCacheEntry());
});
} finally {
commitLatch.countDown();
}
assertTrue(action.get(WAIT_TIMEOUT, TimeUnit.SECONDS));
assertNull(synchronizationException.get());
return entryRef;
}
Aggregations