List of usage examples for org.springframework.transaction.support TransactionTemplate setPropagationBehavior
public final void setPropagationBehavior(int propagationBehavior)
From source file:org.wallride.service.UserService.java
@CacheEvict(value = WallRideCacheConfiguration.USER_CACHE, allEntries = true) @Transactional(propagation = Propagation.NOT_SUPPORTED) public List<User> bulkDeleteUser(UserBulkDeleteRequest bulkDeleteForm, BindingResult result) { List<User> users = new ArrayList<>(); for (long id : bulkDeleteForm.getIds()) { final UserDeleteRequest deleteRequest = new UserDeleteRequest.Builder().id(id).build(); final BeanPropertyBindingResult r = new BeanPropertyBindingResult(deleteRequest, "request"); r.setMessageCodesResolver(messageCodesResolver); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); User user = null;/* w ww. jav a 2 s . c o m*/ try { user = transactionTemplate.execute(new TransactionCallback<User>() { public User doInTransaction(TransactionStatus status) { try { return deleteUser(deleteRequest, r); } catch (BindException e) { throw new RuntimeException(e); } } }); users.add(user); } catch (Exception e) { logger.debug("Errors: {}", r); result.addAllErrors(r); } } return users; }
From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java
@Test(expected = TransactionSystemException.class) public void testTransactionCommitError() throws FcrepoOperationFailedException { final String baseUrl = "http://localhost:8080/rest"; final String tx = "tx:1234567890"; final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT); final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION); final FcrepoTransactionManager txMgr = new FcrepoTransactionManager(); txMgr.setBaseUrl(baseUrl);/*w w w. ja va 2 s . c o m*/ TestUtils.setField(txMgr, "fcrepoClient", mockClient); final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr); final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.afterPropertiesSet(); when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder); when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2); when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201, singletonMap("Location", singletonList(baseUrl + "/" + tx)), null)); when(mockPostBuilder2.perform()) .thenThrow(new FcrepoOperationFailedException(commitUri, 400, "Bad Request")); DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); final FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction(); assertEquals(tx, txObj.getSessionId()); assertFalse(status.isCompleted()); status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); txMgr.commit(status); }
From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java
@Test(expected = TransactionSystemException.class) public void testTransactionRollbackError() throws FcrepoOperationFailedException { final String baseUrl = "http://localhost:8080/rest"; final String tx = "tx:1234567890"; final URI rollbackUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.ROLLBACK); final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION); final FcrepoTransactionManager txMgr = new FcrepoTransactionManager(); txMgr.setBaseUrl(baseUrl);/*w w w.j a v a 2s . c om*/ TestUtils.setField(txMgr, "fcrepoClient", mockClient); final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr); final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.afterPropertiesSet(); when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder); when(mockClient.post(eq(rollbackUri))).thenReturn(mockPostBuilder2); when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201, singletonMap("Location", singletonList(baseUrl + "/" + tx)), null)); when(mockPostBuilder2.perform()) .thenThrow(new FcrepoOperationFailedException(rollbackUri, 400, "Bad Request")); DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); final FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction(); assertEquals(tx, txObj.getSessionId()); assertFalse(status.isCompleted()); status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); txMgr.rollback(status); }
From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java
@Test public void testTransactionCommit() throws FcrepoOperationFailedException { final String baseUrl = "http://localhost:8080/rest"; final String tx = "tx:1234567890"; final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT); final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION); final FcrepoTransactionManager txMgr = new FcrepoTransactionManager(); txMgr.setBaseUrl(baseUrl);//from ww w . j av a 2 s . c om TestUtils.setField(txMgr, "fcrepoClient", mockClient); final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr); final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.afterPropertiesSet(); when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder); when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2); when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201, singletonMap("Location", singletonList(baseUrl + "/" + tx)), null)); when(mockPostBuilder2.perform()).thenReturn(new FcrepoResponse(commitUri, 201, emptyMap(), null)); DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction(); assertEquals(tx, txObj.getSessionId()); assertFalse(status.isCompleted()); status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); txMgr.commit(status); txObj = (FcrepoTransactionObject) status.getTransaction(); assertNull(txObj.getSessionId()); assertTrue(status.isCompleted()); }
From source file:org.fcrepo.camel.FcrepoTransactionManagerTest.java
@Test public void testTransactionRollback() throws FcrepoOperationFailedException { final String baseUrl = "http://localhost:8080/rest"; final String tx = "tx:1234567890"; final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT); final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION); final FcrepoTransactionManager txMgr = new FcrepoTransactionManager(); txMgr.setBaseUrl(baseUrl);/*from www . j a v a 2 s.co m*/ TestUtils.setField(txMgr, "fcrepoClient", mockClient); final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr); final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); transactionTemplate.afterPropertiesSet(); when(mockClient.post(eq(beginUri))).thenReturn(mockPostBuilder); when(mockClient.post(eq(commitUri))).thenReturn(mockPostBuilder2); when(mockPostBuilder.perform()).thenReturn(new FcrepoResponse(beginUri, 201, singletonMap("Location", singletonList(baseUrl + "/" + tx)), null)); when(mockPostBuilder2.perform()).thenReturn(new FcrepoResponse(commitUri, 201, emptyMap(), null)); DefaultTransactionStatus status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); FcrepoTransactionObject txObj = (FcrepoTransactionObject) status.getTransaction(); assertEquals(tx, txObj.getSessionId()); assertFalse(status.isCompleted()); status = (DefaultTransactionStatus) txMgr.getTransaction(txDef); txMgr.rollback(status); txObj = (FcrepoTransactionObject) status.getTransaction(); assertNull(txObj.getSessionId()); assertTrue(status.isCompleted()); }
From source file:org.ambraproject.struts2.TransactionInterceptor.java
public String intercept(final ActionInvocation actionInvocation) throws Exception { final Action action = (Action) actionInvocation.getAction(); final ActionProxy actionProxy = actionInvocation.getProxy(); final String methodName = actionProxy.getMethod(); if (getAnnotation(action.getClass(), methodName, ManualTransactionManagement.class) != null) { //Method is annotated tellling us not to manage a transaction for it log.debug(//from w w w . j av a2s. c o m "Not managing transaction for " + action.getClass().getSimpleName() + "." + methodName + "()"); return actionInvocation.invoke(); } if (log.isDebugEnabled()) { log.debug("Intercepted " + action.getClass().getSimpleName() + "." + methodName + "()"); } final Transactional transactionalAnnotation = getAnnotation(action.getClass(), methodName, Transactional.class); TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); if (transactionalAnnotation != null) { txTemplate.setReadOnly(transactionalAnnotation.readOnly()); txTemplate.setTimeout(transactionalAnnotation.timeout()); txTemplate.setIsolationLevel(transactionalAnnotation.isolation().value()); txTemplate.setPropagationBehavior(transactionalAnnotation.propagation().value()); } CallbackResult callbackResult = (CallbackResult) txTemplate.execute(new TransactionCallback() { public CallbackResult doInTransaction(TransactionStatus transactionStatus) { CallbackResult result = new CallbackResult(); try { String actionResult = actionInvocation.invoke(); result.setResult(actionResult); //Rollback for Action responses indicating failure for (String response : new String[] { Action.ERROR, Action.INPUT, Action.LOGIN }) { if (response.equalsIgnoreCase(actionResult) && !transactionStatus.isRollbackOnly()) { log.debug("Rolling back action " + action.getClass().getSimpleName() + " due to result: " + actionResult); transactionStatus.setRollbackOnly(); break; } } } catch (Exception e) { /* * Callback does not throw exception. We need to pass Exception object in the return * parameter so we can throw it in the calling method. */ boolean noRollback = false; if (transactionalAnnotation != null && transactionalAnnotation.noRollbackFor() != null) { for (Class<? extends Throwable> exception : transactionalAnnotation.noRollbackFor()) { if (exception.isInstance(e)) { noRollback = true; break; } } } if (!noRollback && transactionalAnnotation != null && transactionalAnnotation.rollbackFor() != null) { for (Class<? extends Throwable> exception : transactionalAnnotation.rollbackFor()) { if (exception.isInstance(e)) { log.debug("Caught exception, rolling back action invocation " + action.getClass().getSimpleName()); transactionStatus.setRollbackOnly(); break; } } } result.setException(e); } return result; } }); if (callbackResult.getException() != null) throw callbackResult.getException(); return callbackResult.getResult(); }
From source file:ca.uhn.fhir.jpa.term.BaseHapiTerminologySvc.java
private void processReindexing() { if (System.currentTimeMillis() < myNextReindexPass) { return;/*from w w w . j a v a 2 s .c o m*/ } TransactionTemplate tt = new TransactionTemplate(myTransactionMgr); tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus theArg0) { int maxResult = 1000; Page<TermConcept> resources = myConceptDao .findResourcesRequiringReindexing(new PageRequest(0, maxResult)); if (resources.hasContent() == false) { myNextReindexPass = System.currentTimeMillis() + DateUtils.MILLIS_PER_MINUTE; return; } ourLog.info("Indexing {} / {} concepts", resources.getContent().size(), resources.getTotalElements()); int count = 0; StopWatch stopwatch = new StopWatch(); for (TermConcept resourceTable : resources) { saveConcept(resourceTable); count++; } ourLog.info("Indexed {} / {} concepts in {}ms - Avg {}ms / resource", new Object[] { count, resources.getContent().size(), stopwatch.getMillis(), stopwatch.getMillisPerOperation(count) }); } }); }
From source file:com.goodhuddle.huddle.service.impl.MemberServiceImpl.java
@Override public ImportMembersResults importMembers(MultipartFile multipartFile, Long[] tagIds) { final ImportMembersResults results = new ImportMembersResults(); results.setSuccess(true);/* ww w.j a v a 2 s. c om*/ try { final Huddle huddle = huddleService.getHuddle(); if (!multipartFile.isEmpty()) { File file = fileStore.getTempFile(fileStore.createTempFile(multipartFile.getBytes())); final CSVReader reader = new CSVReader(new FileReader(file)); if (reader.readNext() != null) { // skipped header row final List<Tag> tags = new ArrayList<>(); if (tagIds != null) { for (Long tagId : tagIds) { tags.add(tagRepository.findByHuddleAndId(huddle, tagId)); } } boolean done = false; while (!done) { TransactionTemplate txTemplate = new TransactionTemplate(txManager); txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); done = txTemplate.execute(new TransactionCallback<Boolean>() { @Override public Boolean doInTransaction(TransactionStatus status) { try { int i = 0; String[] nextLine; while ((nextLine = reader.readNext()) != null) { String email = nextLine[0]; String firstName = nextLine[1]; String lastName = nextLine[2]; String postCode = nextLine[3]; String phone = nextLine[4]; Member member = memberRepository.findByHuddleAndEmailIgnoreCase(huddle, email); if (member != null) { member.setFirstName(firstName); member.setLastName(lastName); member.setPostCode(postCode); member.setPhone(phone); List<Tag> memberTags = member.getTags(); if (memberTags == null) { memberTags = new ArrayList<>(); } outer: for (Tag tag : tags) { for (Tag memberTag : memberTags) { if (memberTag.getId() == member.getId()) { break outer; } } memberTags.add(tag); } member.setTags(memberTags); memberRepository.save(member); results.setNumUpdated(results.getNumUpdated() + 1); } else { member = new Member(); member.setHuddle(huddle); member.setEmail(email); member.setFirstName(firstName); member.setLastName(lastName); member.setPostCode(postCode); member.setPhone(phone); member.setTags(tags); memberRepository.save(member); results.setNumImported(results.getNumImported() + 1); } if (i++ > 30) { return false; } } return true; } catch (IOException e) { log.error("Error importing file: " + e, e); results.setSuccess(false); results.setErrorMessage("Error importing file: " + e); return true; } } }); } } } else { log.info("Empty file was uploaded"); results.setSuccess(false); results.setErrorMessage("Empty file was uploaded"); } } catch (IOException e) { results.setSuccess(false); results.setErrorMessage("Error uploading file: " + e); } return results; }
From source file:org.wallride.service.PageService.java
@CacheEvict(value = WallRideCacheConfiguration.PAGE_CACHE, allEntries = true) @Transactional(propagation = Propagation.NOT_SUPPORTED) public List<Page> bulkDeletePage(PageBulkDeleteRequest bulkDeleteRequest, BindingResult result) { List<Page> pages = new ArrayList<>(); for (long id : bulkDeleteRequest.getIds()) { final PageDeleteRequest deleteRequest = new PageDeleteRequest.Builder().id(id) .language(bulkDeleteRequest.getLanguage()).build(); final BeanPropertyBindingResult r = new BeanPropertyBindingResult(deleteRequest, "request"); r.setMessageCodesResolver(messageCodesResolver); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); Page page = null;/*w ww . j a v a 2 s . c o m*/ try { page = transactionTemplate.execute(new TransactionCallback<Page>() { public Page doInTransaction(TransactionStatus status) { try { return deletePage(deleteRequest, r); } catch (BindException e) { throw new RuntimeException(e); } } }); pages.add(page); } catch (Exception e) { logger.debug("Errors: {}", r); result.addAllErrors(r); } } return pages; }
From source file:org.wallride.service.ArticleService.java
@Transactional(propagation = Propagation.NOT_SUPPORTED) @CacheEvict(value = WallRideCacheConfiguration.ARTICLE_CACHE, allEntries = true) public List<Article> bulkDeleteArticle(ArticleBulkDeleteRequest bulkDeleteRequest, BindingResult result) { List<Article> articles = new ArrayList<>(); for (long id : bulkDeleteRequest.getIds()) { final ArticleDeleteRequest deleteRequest = new ArticleDeleteRequest.Builder().id(id) .language(bulkDeleteRequest.getLanguage()).build(); final BeanPropertyBindingResult r = new BeanPropertyBindingResult(deleteRequest, "request"); r.setMessageCodesResolver(messageCodesResolver); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); Article article = null;//from w ww. j a v a 2 s . c o m try { article = transactionTemplate.execute(new TransactionCallback<Article>() { public Article doInTransaction(TransactionStatus status) { try { return deleteArticle(deleteRequest, r); } catch (BindException e) { throw new RuntimeException(e); } } }); articles.add(article); } catch (Exception e) { logger.debug("Errors: {}", r); result.addAllErrors(r); } } return articles; }