List of usage examples for org.springframework.transaction.support TransactionTemplate execute
@Override @Nullable public <T> T execute(TransactionCallback<T> action) throws TransactionException
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 ww .j a v a2 s . co 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);//from w w w . jav a2 s.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.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/*www. ja va 2s . 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")); }//from w w w. ja va2 s . co 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)); }
From source file:test.jdbc.datasource.DataSourceInitializer.java
private void doExecuteScript(final Resource scriptResource) { if (scriptResource == null || !scriptResource.exists()) return;/*ww w .j av a 2 s.c o m*/ final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String[] scripts; try { String[] list = StringUtils.delimitedListToStringArray( stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";"); scripts = list; } catch (IOException e) { throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); } for (int i = 0; i < scripts.length; i++) { final String script = scripts[i].trim(); TransactionTemplate transactionTemplate = new TransactionTemplate( new DataSourceTransactionManager(dataSource)); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { if (StringUtils.hasText(script)) { try { jdbcTemplate.execute(script); } catch (DataAccessException e) { if (!script.toUpperCase().startsWith("DROP")) { throw e; } } } return null; } }); } }
From source file:ubc.pavlab.aspiredb.server.dao.CNVDaoTest.java
@Before public void createIndividualAndCNVs() { TransactionTemplate tt = new TransactionTemplate(transactionManager); tt.execute(new TransactionCallbackWithoutResult() { @Override//from ww w . ja v a 2 s . c om public void doInTransactionWithoutResult(TransactionStatus status) { individual = new Subject(); String patientId = "test_patient"; individual.setPatientId(patientId); GenomicLocation genomicLocation1 = new GenomicLocation("40", 5050000, 5450000); cnv1 = new CNV(); cnv1.setLocation(genomicLocation1); cnv1.setCopyNumber(1); cnv1.setType(CnvType.valueOf("LOSS")); cnvDao.create(cnv1); individual.addVariant(cnv1); Phenotype ph = new Phenotype(); ph.setName("Test Phenotype"); ph.setValue("1234"); individual.addPhenotype(ph); individualDao.create(individual); } }); }
From source file:ubc.pavlab.aspiredb.server.dao.CNVDaoTest.java
@Test public void testCreate() { TransactionTemplate tt = new TransactionTemplate(transactionManager); tt.execute(new TransactionCallbackWithoutResult() { @Override/*from ww w . j a va 2 s.c o m*/ public void doInTransactionWithoutResult(TransactionStatus status) { // Just a stub to test the plumbing. CNV cnv = new CNV(); cnvDao.create(cnv); } }); }
From source file:ubc.pavlab.aspiredb.server.dao.IndelDaoTest.java
@Before public void createIndividualAndIndels() { TransactionTemplate tt = new TransactionTemplate(transactionManager); tt.execute(new TransactionCallbackWithoutResult() { @Override/* w w w. j a v a 2s . co m*/ public void doInTransactionWithoutResult(TransactionStatus status) { individual = new Subject(); String patientId = "test_patient"; individual.setPatientId(patientId); Indel indel = testObjectHelper.createDetachedTestIndelObject(); indelDao.create(indel); individual.addVariant(indel); Phenotype ph = new Phenotype(); ph.setName("Test Phenotype"); ph.setValue("1234"); individual.addPhenotype(ph); individualDao.create(individual); } }); }
From source file:ubc.pavlab.aspiredb.server.dao.IndelDaoTest.java
@Test public void testCreate() { TransactionTemplate tt = new TransactionTemplate(transactionManager); tt.execute(new TransactionCallbackWithoutResult() { @Override/*from ww w . jav a 2s. c o m*/ public void doInTransactionWithoutResult(TransactionStatus status) { // Just a stub to test the plumbing. Indel indel = new Indel(); indelDao.create(indel); } }); }
From source file:ubc.pavlab.aspiredb.server.dao.SNVDaoTest.java
@Test public void testCreate() { TransactionTemplate tt = new TransactionTemplate(transactionManager); tt.execute(new TransactionCallbackWithoutResult() { @Override//w w w.j a va2s . c o m public void doInTransactionWithoutResult(TransactionStatus status) { // Just a stub to test the plumbing. testObjectHelper.createPersistentTestSNVObject(); } }); }