use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.
the class PlatformTestRunnerTransactionTestFixture method before.
@Before
public void before() {
m_txnMember = mock(ITransactionMember.class);
when(m_txnMember.getMemberId()).thenReturn("mock txn member");
when(m_txnMember.needsCommit()).thenReturn(true);
when(m_txnMember.commitPhase1()).thenReturn(true);
ITransaction txn = ITransaction.CURRENT.get();
assertNotNull(txn);
txn.registerMember(m_txnMember);
}
use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.
the class InvocationContextTest method testWithException.
@Test
public void testWithException() {
// Unregister JUnit exception handler
BEANS.getBeanManager().unregisterBean(BEANS.getBeanManager().getBean(JUnitExceptionHandler.class));
final Holder<ITransaction> currentTransaction = new Holder<>();
final Holder<ITransaction> invocationTransaction = new Holder<>();
final Holder<IServerSession> invocationServerSession = new Holder<>();
final Holder<Exception> callableException = new Holder<>();
// simulate that 'webMethod' throws an exception.
final RuntimeException exception = new RuntimeException();
doThrow(exception).when(m_port).webMethod();
try {
ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
TransactionScope.REQUIRES_NEW).run(new IRunnable() {
@Override
public void run() throws Exception {
currentTransaction.setValue(ITransaction.CURRENT.get());
InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
invocationContext.whenCommit(m_commitListener);
invocationContext.whenRollback(m_rollbackListener);
invocationContext.whenInvoke(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
invocationTransaction.setValue(ITransaction.CURRENT.get());
invocationServerSession.setValue(ServerSessionProvider.currentSession());
return method.invoke(proxy, args);
}
});
// run the test
try {
invocationContext.getPort().webMethod();
} catch (Exception e) {
callableException.setValue(e);
throw e;
}
}
});
fail("RuntimeException expected");
} catch (RuntimeException e) {
// NOOP
}
assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
verify(m_port).webMethod();
verify(m_commitListener, never()).onCommitPhase1();
verify(m_commitListener, never()).onCommitPhase2();
verify(m_rollbackListener).onRollback();
assertSame(callableException.getValue(), exception);
}
use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.
the class InvocationContextTest method testWithSuccess.
@Test
public void testWithSuccess() {
final Holder<ITransaction> currentTransaction = new Holder<>();
final Holder<ITransaction> invocationTransaction = new Holder<>();
final Holder<IServerSession> invocationServerSession = new Holder<>();
ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
TransactionScope.REQUIRES_NEW).run(new IRunnable() {
@Override
public void run() throws Exception {
currentTransaction.setValue(ITransaction.CURRENT.get());
InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
invocationContext.whenCommit(m_commitListener);
invocationContext.whenRollback(m_rollbackListener);
invocationContext.whenInvoke(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
invocationTransaction.setValue(ITransaction.CURRENT.get());
invocationServerSession.setValue(ServerSessionProvider.currentSession());
return method.invoke(proxy, args);
}
});
// run the test
invocationContext.getPort().webMethod();
}
});
assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
verify(m_port).webMethod();
verify(m_commitListener).onCommitPhase1();
verify(m_commitListener).onCommitPhase2();
verify(m_rollbackListener, never()).onRollback();
}
use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.
the class ClusterSynchronizationService method getTransaction.
/**
* @return transaction member for publishing messages within a transaction
*/
protected ClusterSynchTransactionMember getTransaction() {
ITransaction tx = Assertions.assertNotNull(ITransaction.CURRENT.get(), "Transaction required");
ClusterSynchTransactionMember m = (ClusterSynchTransactionMember) tx.getMember(TRANSACTION_MEMBER_ID);
if (m == null) {
m = new ClusterSynchTransactionMember(TRANSACTION_MEMBER_ID);
tx.registerMember(m);
}
return m;
}
use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.
the class AbstractSqlService method getTransaction.
protected Connection getTransaction() {
ITransaction tx = Assertions.assertNotNull(ITransaction.CURRENT.get(), "Transaction required");
SqlTransactionMember member = (SqlTransactionMember) tx.getMember(getTransactionMemberId());
if (member == null) {
@SuppressWarnings("resource") Connection connection = leaseConnection();
member = new SqlTransactionMember(getTransactionMemberId(), connection);
tx.registerMember(member);
// this is the start of the transaction
execBeginTransaction();
}
return member.getConnection();
}
Aggregations