Search in sources :

Example 1 with ITransaction

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);
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) ITransactionMember(org.eclipse.scout.rt.platform.transaction.ITransactionMember) Before(org.junit.Before)

Example 2 with ITransaction

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);
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 3 with ITransaction

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();
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 4 with ITransaction

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;
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction)

Example 5 with ITransaction

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();
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) Connection(java.sql.Connection)

Aggregations

ITransaction (org.eclipse.scout.rt.platform.transaction.ITransaction)37 Test (org.junit.Test)20 ISession (org.eclipse.scout.rt.shared.ISession)13 HashMap (java.util.HashMap)7 ITransactionMember (org.eclipse.scout.rt.platform.transaction.ITransactionMember)5 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)5 JUnitServerSession1 (org.eclipse.scout.rt.testing.server.runner.ServerTestRunnerDifferentSessionTest.JUnitServerSession1)4 JUnitServerSession (org.eclipse.scout.rt.testing.server.runner.ServerTestRunnerDifferentSubjectTest.JUnitServerSession)4 JUnitServerSession (org.eclipse.scout.rt.testing.server.runner.ServerTestRunnerSameSessionTest.JUnitServerSession)4 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 AfterClass (org.junit.AfterClass)3 BeforeClass (org.junit.BeforeClass)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 WebMethod (javax.jws.WebMethod)2 BooleanHolder (org.eclipse.scout.rt.platform.holders.BooleanHolder)2 Holder (org.eclipse.scout.rt.platform.holders.Holder)2 IServerSession (org.eclipse.scout.rt.server.IServerSession)2 AbstractSqlTransactionMember (org.eclipse.scout.rt.server.jdbc.AbstractSqlTransactionMember)2 Connection (java.sql.Connection)1