use of javax.transaction.Transaction in project wildfly by wildfly.
the class CMTTxInterceptor method requiresNew.
protected Object requiresNew(InterceptorContext invocation, final EJBComponent component, final int timeout) throws Exception {
final TransactionManager tm = component.getTransactionManager();
if (timeout != -1) {
tm.setTransactionTimeout(timeout);
}
Transaction tx = tm.getTransaction();
if (tx != null) {
tm.suspend();
try {
return invokeInOurTx(invocation, tm, component);
} finally {
tm.resume(tx);
}
} else {
return invokeInOurTx(invocation, tm, component);
}
}
use of javax.transaction.Transaction in project wildfly by wildfly.
the class InfinispanBatcherTestCase method resumeBatch.
@Test
public void resumeBatch() throws Exception {
TransactionBatch batch = mock(TransactionBatch.class);
Transaction tx = mock(Transaction.class);
when(batch.getTransaction()).thenReturn(tx);
try (BatchContext context = this.batcher.resumeBatch(batch)) {
verify(this.tm, never()).suspend();
verify(this.tm).resume(tx);
reset(this.tm);
assertSame(batch, InfinispanBatcher.CURRENT_BATCH.get());
}
verify(this.tm).suspend();
verify(this.tm, never()).resume(any());
assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
use of javax.transaction.Transaction in project wildfly by wildfly.
the class InfinispanBatcher method resumeBatch.
@Override
public BatchContext resumeBatch(TransactionBatch batch) {
TransactionBatch existingBatch = CURRENT_BATCH.get();
// Trivial case - nothing to suspend/resume
if (batch == existingBatch)
return PASSIVE_BATCH_CONTEXT;
Transaction tx = (batch != null) ? batch.getTransaction() : null;
// Non-tx case, just swap thread local
if ((batch == null) || (tx == null)) {
CURRENT_BATCH.set(batch);
return () -> {
CURRENT_BATCH.set(existingBatch);
};
}
try {
if (existingBatch != null) {
Transaction existingTx = this.tm.suspend();
if (existingBatch.getTransaction() != existingTx) {
throw new IllegalStateException();
}
}
this.tm.resume(tx);
CURRENT_BATCH.set(batch);
return () -> {
try {
this.tm.suspend();
if (existingBatch != null) {
try {
this.tm.resume(existingBatch.getTransaction());
CURRENT_BATCH.set(existingBatch);
} catch (InvalidTransactionException e) {
throw new CacheException(e);
}
} else {
CURRENT_BATCH.remove();
}
} catch (SystemException e) {
throw new CacheException(e);
}
};
} catch (SystemException | InvalidTransactionException e) {
throw new CacheException(e);
}
}
use of javax.transaction.Transaction in project wildfly by wildfly.
the class InfinispanBatcher method createBatch.
@Override
public TransactionBatch createBatch() {
if (this.tm == null)
return NON_TX_BATCH;
TransactionBatch batch = CURRENT_BATCH.get();
if (batch != null) {
return batch.interpose();
}
try {
this.tm.suspend();
this.tm.begin();
Transaction tx = this.tm.getTransaction();
tx.registerSynchronization(CURRENT_BATCH_REMOVER);
batch = new InfinispanBatch(tx);
CURRENT_BATCH.set(batch);
return batch;
} catch (RollbackException | SystemException | NotSupportedException e) {
throw new CacheException(e);
}
}
use of javax.transaction.Transaction in project wildfly by wildfly.
the class InfinispanBatcherTestCase method createBatchDiscard.
@Test
public void createBatchDiscard() 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());
batch.discard();
} finally {
capturedSync.getValue().afterCompletion(Status.STATUS_ROLLEDBACK);
}
verify(tx, never()).commit();
verify(tx).rollback();
assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Aggregations