Search in sources :

Example 31 with ITransaction

use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.

the class AbstractTransactionalMap method getTransaction.

@SuppressWarnings("unchecked")
protected <TM extends Map<K, V> & ITransactionMember> TM getTransaction(boolean createIfNotExist) {
    ITransaction t = ITransaction.CURRENT.get();
    if (t == null) {
        return null;
    }
    TM m = (TM) t.getMember(getTransactionMemberId());
    if (m == null && createIfNotExist) {
        m = createMapTransactionMember();
        t.registerMember(m);
    }
    return m;
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction)

Example 32 with ITransaction

use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.

the class JmsMomImplementorTest method testPublishTransactional.

@Test
public void testPublishTransactional() throws InterruptedException {
    final Capturer<Person> capturer = new Capturer<>();
    Person person = new Person();
    person.setLastname("smith");
    person.setFirstname("anna");
    ITransaction tx = BEANS.get(ITransaction.class);
    ITransaction.CURRENT.set(tx);
    IDestination<Person> queue = MOM.newDestination("test/mom/testPublishTransactional", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(ObjectMarshaller.class)));
    MOM.publish(JmsTestMom.class, queue, person, MOM.newPublishInput().withTransactional(true));
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Person>() {

        @Override
        public void onMessage(IMessage<Person> message) {
            capturer.set(message.getTransferObject());
        }
    }));
    try {
        capturer.get(2, TimeUnit.SECONDS);
        fail();
    } catch (TimedOutError e) {
    // expected
    }
    tx.commitPhase1();
    tx.commitPhase2();
    try {
        capturer.get(5, TimeUnit.SECONDS);
    } catch (TimedOutError e) {
        fail();
    } finally {
        tx.release();
    }
    // Verify
    Person testee = capturer.get();
    assertEquals("smith", testee.getLastname());
    assertEquals("anna", testee.getFirstname());
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) IMessage(org.eclipse.scout.rt.mom.api.IMessage) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Example 33 with ITransaction

use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.

the class AbstractSqlService method getStatementCache.

/**
 * @return the statement cache used for this {@link ITransaction} transaction
 */
protected final IStatementCache getStatementCache() {
    ITransaction tx = Assertions.assertNotNull(ITransaction.CURRENT.get(), "Transaction required");
    IStatementCache res = (IStatementCache) tx.getMember(PreparedStatementCache.TRANSACTION_MEMBER_ID);
    if (res == null) {
        res = new PreparedStatementCache(getJdbcStatementCacheSize());
        tx.registerMember((ITransactionMember) res);
    }
    return res;
}
Also used : PreparedStatementCache(org.eclipse.scout.rt.server.jdbc.internal.exec.PreparedStatementCache) ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction)

Example 34 with ITransaction

use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.

the class PooledPortProvider method provide.

/**
 * Returns the same port that was already created within the current transaction (and therefore within the current
 * thread) or creates a new one, if none has been used so far.
 */
@Override
public PORT provide() {
    final ITransaction txn = Assertions.assertNotNull(ITransaction.CURRENT.get());
    final String txnMemberId = m_portTypeClazz.getName() + ".transaction";
    @SuppressWarnings("unchecked") P_PooledPortTransactionMember member = (P_PooledPortTransactionMember) txn.getMember(txnMemberId);
    if (member != null) {
        PORT port = member.getPort();
        // reset request context if port is used another time within the same transaction
        BEANS.get(JaxWsImplementorSpecifics.class).resetRequestContext(port);
        return port;
    }
    // create new port
    final PORT port = m_portPool.lease();
    member = new P_PooledPortTransactionMember(port, txnMemberId);
    txn.registerMember(member);
    return port;
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) JaxWsImplementorSpecifics(org.eclipse.scout.rt.server.jaxws.implementor.JaxWsImplementorSpecifics)

Example 35 with ITransaction

use of org.eclipse.scout.rt.platform.transaction.ITransaction in project scout.rt by eclipse.

the class RunContextTest method testCopy.

@Test
public void testCopy() {
    final Subject subject = newSubject("john");
    final ITransaction tx = mock(ITransaction.class);
    final ITransactionMember txMember1 = mock(ITransactionMember.class);
    when(txMember1.getMemberId()).thenReturn("txMember1");
    final ITransactionMember txMember2 = mock(ITransactionMember.class);
    when(txMember2.getMemberId()).thenReturn("txMember2");
    final RunMonitor monitor = new RunMonitor();
    // prepare the RunContext to be copied
    RunContext runContext = RunContexts.empty().withProperty("key", "value").withSubject(subject).withLocale(Locale.CANADA_FRENCH).withRunMonitor(monitor).withCorrelationId("cid").withTransaction(tx).withTransactionScope(TransactionScope.MANDATORY).withThreadLocal(THREAD_LOCAL, "thread-local");
    // test
    RunContext copy = runContext.copy();
    // verify
    assertEquals(toSet(runContext.getPropertyMap().iterator()), toSet(copy.getPropertyMap().iterator()));
    assertSame(runContext.getSubject(), copy.getSubject());
    assertSame(runContext.getLocale(), copy.getLocale());
    assertSame(runContext.getRunMonitor(), copy.getRunMonitor());
    assertEquals("cid", runContext.getCorrelationId());
    assertSame(tx, runContext.getTransaction());
    assertEquals("thread-local", runContext.getThreadLocal(THREAD_LOCAL));
    // test running on the copy
    copy.withTransactionScope(TransactionScope.REQUIRED).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertEquals("value", PropertyMap.CURRENT.get().get("key"));
            assertSame(subject, Subject.getSubject(AccessController.getContext()));
            assertEquals(Locale.CANADA_FRENCH, NlsLocale.CURRENT.get());
            assertSame(monitor, RunMonitor.CURRENT.get());
            assertEquals("cid", CorrelationId.CURRENT.get());
            assertSame(tx, ITransaction.CURRENT.get());
            assertEquals("thread-local", THREAD_LOCAL.get());
            RunContexts.copyCurrent().run(new IRunnable() {

                @Override
                public void run() throws Exception {
                    assertEquals("value", PropertyMap.CURRENT.get().get("key"));
                    assertSame(subject, Subject.getSubject(AccessController.getContext()));
                    assertEquals(Locale.CANADA_FRENCH, NlsLocale.CURRENT.get());
                    assertNotSame(monitor, RunMonitor.CURRENT.get());
                    assertEquals("cid", CorrelationId.CURRENT.get());
                    assertSame(tx, ITransaction.CURRENT.get());
                    assertEquals("thread-local", THREAD_LOCAL.get());
                }
            });
            RunContexts.empty().run(new IRunnable() {

                @Override
                public void run() throws Exception {
                    assertNull(PropertyMap.CURRENT.get().get("key"));
                    assertNull(Subject.getSubject(AccessController.getContext()));
                    assertNull(NlsLocale.CURRENT.get());
                    assertNotSame(monitor, RunMonitor.CURRENT.get());
                    assertNull(CorrelationId.CURRENT.get());
                    assertNotSame(tx, ITransaction.CURRENT.get());
                    assertEquals("thread-local", THREAD_LOCAL.get());
                }
            });
        }
    });
    // test copy of transaction members
    runContext.withTransactionScope(TransactionScope.REQUIRES_NEW).withTransactionMember(txMember1).withTransactionMember(txMember2).copy().run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(txMember1, ITransaction.CURRENT.get().getMember("txMember1"));
            assertSame(txMember2, ITransaction.CURRENT.get().getMember("txMember2"));
        }
    });
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) ITransactionMember(org.eclipse.scout.rt.platform.transaction.ITransactionMember) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

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