List of usage examples for org.springframework.transaction.support TransactionTemplate TransactionTemplate
public TransactionTemplate(PlatformTransactionManager transactionManager)
From source file:org.springframework.data.neo4j.repository.SpatialGraphRepositoryTest.java
@Before public void setUp() throws Exception { new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { testTeam = new TestTeam(); testTeam.createSDGTeam(personRepository, groupRepository, friendshipRepository); }//from w w w. j av a 2 s .c o m }); }
From source file:org.springframework.data.neo4j.support.Neo4jTemplate.java
@Override public <T> T exec(final GraphCallback<T> callback) { if (infrastructure.getTransactionManager() == null) return doExecute(callback); TransactionTemplate template = new TransactionTemplate(infrastructure.getTransactionManager()); return template.execute(new TransactionCallback<T>() { @Override/*from w ww .ja va 2 s . com*/ public T doInTransaction(TransactionStatus status) { return doExecute(callback); } }); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testCommitWithManager() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix")); syncProcessor.setBeforeCommitChannel(queueChannel); syncProcessor.setAfterCommitChannel(queueChannel); syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);//from w w w .ja v a2 s .c om adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager .getResource(this); holder.addAttribute("baz", "qux"); holder.addAttribute("bix", "qox"); return message; } }); doPoll(adapter); return null; }); Message<?> beforeCommitMessage = queueChannel.receive(1000); assertNotNull(beforeCommitMessage); assertEquals("qox", beforeCommitMessage.getPayload()); Message<?> afterCommitMessage = queueChannel.receive(1000); assertNotNull(afterCommitMessage); assertEquals("qux", afterCommitMessage.getPayload()); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testRollbackWithManager() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); try {/*from w w w. ja v a2s .c o m*/ transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor); adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)) .addAttribute("baz", "qux"); return message; } }); doPoll(adapter); throw new RuntimeException("Force rollback"); }); } catch (Exception e) { assertEquals("Force rollback", e.getMessage()); } Message<?> rollbackMessage = queueChannel.receive(1000); assertNotNull(rollbackMessage); assertEquals("qux", rollbackMessage.getPayload()); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testRollbackWithManagerUsingStatus() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);// w w w. j av a2s . com adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)) .addAttribute("baz", "qux"); return message; } }); doPoll(adapter); status.setRollbackOnly(); return null; }); Message<?> rollbackMessage = queueChannel.receive(1000); assertNotNull(rollbackMessage); assertEquals("qux", rollbackMessage.getPayload()); }
From source file:org.springframework.integration.jdbc.mysql.MySqlJdbcMessageStoreMultipleChannelTests.java
@After public void afterTest() { new TransactionTemplate(this.transactionManager).execute(new TransactionCallback<Void>() { public Void doInTransaction(TransactionStatus status) { final int deletedGroupToMessageRows = jdbcTemplate.update("delete from INT_GROUP_TO_MESSAGE"); final int deletedMessages = jdbcTemplate.update("delete from INT_MESSAGE"); final int deletedMessageGroups = jdbcTemplate.update("delete from INT_MESSAGE_GROUP"); LOG.info(String.format( "Cleaning Database - Deleted Messages: %s, " + "Deleted GroupToMessage Rows: %s, Deleted Message Groups: %s", deletedMessages, deletedGroupToMessageRows, deletedMessageGroups)); return null; }/* w w w . ja va 2 s . c o m*/ }); }
From source file:org.springframework.integration.jdbc.mysql.MySqlJdbcMessageStoreMultipleChannelTests.java
@Test public void testSendAndActivateTransactionalSend() throws Exception { new TransactionTemplate(this.transactionManager).execute(new TransactionCallback<Void>() { public Void doInTransaction(TransactionStatus status) { requestChannel.send(MessageBuilder.withPayload("Hello ").build()); return null; }/*from ww w. ja v a2s . c om*/ }); assertTrue("countDownLatch1 was " + countDownLatch1.getCount(), countDownLatch1.await(10000, TimeUnit.MILLISECONDS)); assertTrue("countDownLatch2 was " + countDownLatch2.getCount(), countDownLatch2.await(10000, TimeUnit.MILLISECONDS)); assertTrue("Wrong Sequence Number handled.", success.get()); assertNull(errorChannel.receive(0)); }
From source file:org.springframework.integration.jdbc.mysql.MySqlJdbcMessageStoreTests.java
@After public void afterTest() { final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); new TransactionTemplate(this.transactionManager).execute(new TransactionCallback<Void>() { public Void doInTransaction(TransactionStatus status) { final int deletedGroupToMessageRows = jdbcTemplate.update("delete from INT_GROUP_TO_MESSAGE"); final int deletedMessages = jdbcTemplate.update("delete from INT_MESSAGE"); final int deletedMessageGroups = jdbcTemplate.update("delete from INT_MESSAGE_GROUP"); LOG.info(String.format( "Cleaning Database - Deleted Messages: %s, " + "Deleted GroupToMessage Rows: %s, Deleted Message Groups: %s", deletedMessages, deletedGroupToMessageRows, deletedMessageGroups)); return null; }/* ww w . j a va2 s . co m*/ }); }
From source file:org.springframework.integration.jdbc.store.channel.AbstractTxTimeoutMessageStoreTests.java
public void test() throws InterruptedException { int maxMessages = 10; int maxWaitTime = 30000; final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setIsolationLevel(Isolation.READ_COMMITTED.value()); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); for (int i = 1; i <= maxMessages; ++i) { final String message = "TEST MESSAGE " + i; log.info("Sending message: " + message); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override/*from ww w . j av a2 s . c o m*/ protected void doInTransactionWithoutResult(TransactionStatus status) { inputChannel.send(MessageBuilder.withPayload(message).build()); } }); log.info(String.format("Done sending message %s of %s: %s", i, maxMessages, message)); } log.info("Done sending " + maxMessages + " messages."); Assert.assertTrue(String.format("Contdown latch did not count down from " + "%s to 0 in %sms.", maxMessages, maxWaitTime), testService.await(maxWaitTime)); Thread.sleep(2000); Assert.assertEquals(Integer.valueOf(0), Integer.valueOf(jdbcChannelMessageStore.getSizeOfIdCache())); Assert.assertEquals(Integer.valueOf(maxMessages), Integer.valueOf(testService.getSeenMessages().size())); Assert.assertEquals(Integer.valueOf(0), Integer.valueOf(testService.getDuplicateMessagesCount())); }
From source file:org.springframework.integration.jdbc.store.channel.AbstractTxTimeoutMessageStoreTests.java
public void testInt2993IdCacheConcurrency() throws InterruptedException, ExecutionException { final String groupId = "testInt2993Group"; for (int i = 0; i < 100; i++) { this.jdbcChannelMessageStore.addMessageToGroup(groupId, new GenericMessage<String>("testInt2993Message")); }// w w w . j ava 2 s .c o m ExecutorService executorService = Executors.newCachedThreadPool(); CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(executorService); final int concurrency = 5; final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); for (int i = 0; i < concurrency; i++) { completionService.submit(new Callable<Boolean>() { @Override public Boolean call() throws Exception { for (int i = 0; i < 100; i++) { boolean result = transactionTemplate.execute(new TransactionCallback<Boolean>() { @Override public Boolean doInTransaction(TransactionStatus status) { Message<?> message = null; try { message = jdbcChannelMessageStore.pollMessageFromGroup(groupId); } catch (Exception e) { log.error("IdCache race condition.", e); return false; } try { Thread.sleep(10); } catch (InterruptedException e) { log.error(e); } if (message != null) { jdbcChannelMessageStore .removeFromIdCache(message.getHeaders().getId().toString()); } return true; } }); if (!result) return false; } return true; } }); } for (int j = 0; j < concurrency; j++) { assertTrue(completionService.take().get()); } executorService.shutdown(); assertTrue(executorService.awaitTermination(5, TimeUnit.SECONDS)); }