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;
}
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());
}
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;
}
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;
}
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"));
}
});
}
Aggregations