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;
}
use of javax.transaction.Synchronization in project wildfly by wildfly.
the class InfinispanBatcherTestCase method createNestedBatchClose.
@Test
public void createNestedBatchClose() throws Exception {
Transaction tx = mock(Transaction.class);
ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
when(this.tm.getTransaction()).thenReturn(tx);
try (TransactionBatch outerBatch = this.batcher.createBatch()) {
verify(this.tm).begin();
verify(tx).registerSynchronization(capturedSync.capture());
reset(this.tm);
assertSame(tx, outerBatch.getTransaction());
when(this.tm.getTransaction()).thenReturn(tx);
try (TransactionBatch innerBatch = this.batcher.createBatch()) {
verify(this.tm, never()).begin();
verify(this.tm, never()).suspend();
}
verify(tx, never()).rollback();
verify(tx, never()).commit();
} finally {
capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
}
verify(tx, never()).rollback();
verify(tx).commit();
assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
use of javax.transaction.Synchronization in project wildfly by wildfly.
the class InfinispanBatcherTestCase method createOverlappingBatchClose.
@SuppressWarnings("resource")
@Test
public void createOverlappingBatchClose() throws Exception {
Transaction tx = mock(Transaction.class);
ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
when(this.tm.getTransaction()).thenReturn(tx);
TransactionBatch batch = this.batcher.createBatch();
verify(this.tm).begin();
verify(tx).registerSynchronization(capturedSync.capture());
reset(this.tm);
try {
assertSame(tx, batch.getTransaction());
when(this.tm.getTransaction()).thenReturn(tx);
when(tx.getStatus()).thenReturn(Status.STATUS_ACTIVE);
try (TransactionBatch innerBatch = this.batcher.createBatch()) {
verify(this.tm, never()).begin();
batch.close();
verify(tx, never()).rollback();
verify(tx, never()).commit();
}
} finally {
capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
}
verify(tx, never()).rollback();
verify(tx).commit();
assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
use of javax.transaction.Synchronization in project wildfly by wildfly.
the class SFSB1 method createEmployeeWaitForTxTimeout.
@TransactionTimeout(value = 1, unit = TimeUnit.SECONDS)
public void createEmployeeWaitForTxTimeout(boolean registerTxSync, String name, String address, int id) {
LOGGER.trace("org.jboss.as.test.integration.jpa.mockprovider.txtimeout.createEmployeeWaitForTxTimeout " + "entered, will wait for tx time out to occur");
Employee emp = new Employee();
emp.setId(id);
emp.setAddress(address);
emp.setName(name);
entityManager.persist(emp);
boolean done = false;
if (registerTxSync) {
transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(int status) {
afterCompletionCalledByTMTimeoutThread = TxUtils.isTransactionManagerTimeoutThread();
}
});
}
while (!done) {
try {
Thread.sleep(250);
entityManager.find(Employee.class, id);
int status = transactionSynchronizationRegistry.getTransactionStatus();
switch(status) {
case Status.STATUS_COMMITTED:
throw new RuntimeException("transaction was committed.");
case Status.STATUS_ROLLEDBACK:
LOGGER.trace("tx timed out and rolled back as expected, success case reached.");
done = true;
break;
case Status.STATUS_ACTIVE:
LOGGER.trace("tx is still active, sleep for 250ms and check tx status again.");
break;
default:
LOGGER.trace("tx status = " + status + ", sleep for 250ms and check tx status again.");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
LOGGER.trace("org.jboss.as.test.integration.jpa.mockprovider.txtimeout.createEmployeeWaitForTxTimeout waiting for tx to timeout");
}
}
use of javax.transaction.Synchronization in project wildfly by wildfly.
the class InfinispanBatcherTestCase method createBatchClose.
@Test
public void createBatchClose() throws Exception {
Transaction tx = mock(Transaction.class);
ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
when(this.tm.getTransaction()).thenReturn(tx);
try (TransactionBatch batch = this.batcher.createBatch()) {
verify(this.tm).begin();
verify(tx).registerSynchronization(capturedSync.capture());
assertSame(tx, batch.getTransaction());
} finally {
capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
}
verify(tx).commit();
assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Aggregations