Example usage for org.springframework.transaction TransactionDefinition PROPAGATION_REQUIRED

List of usage examples for org.springframework.transaction TransactionDefinition PROPAGATION_REQUIRED

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionDefinition PROPAGATION_REQUIRED.

Prototype

int PROPAGATION_REQUIRED

To view the source code for org.springframework.transaction TransactionDefinition PROPAGATION_REQUIRED.

Click Source Link

Document

Support a current transaction; create a new one if none exists.

Usage

From source file:org.alfresco.repo.transaction.TransactionServiceImpl.java

/**
 * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED
 *//*from  w  w  w.j a  v a2  s  .co m*/
public UserTransaction getUserTransaction(boolean readOnly, boolean ignoreSystemReadOnly) {
    if (ignoreSystemReadOnly) {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager, (readOnly),
                TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED,
                TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    } else {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager,
                (readOnly | isReadOnly()), TransactionDefinition.ISOLATION_DEFAULT,
                TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    }
}

From source file:org.artifactory.repo.db.importexport.DbRepoImportHandler.java

private void startTransaction() {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    AbstractPlatformTransactionManager txManager = getTransactionManager();
    transactionStartTime = DateTime.now();
    this.transactionStatus = txManager.getTransaction(def);
}

From source file:org.broadleafcommerce.core.order.service.OrderServiceImpl.java

@Override
public Order save(Order order, Boolean priceOrder) throws PricingException {
    //persist the order first
    TransactionStatus status = TransactionUtils.createTransaction("saveOrder",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager);
    try {//w  w  w. j av  a2s  .  co m
        order = persist(order);
        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (RuntimeException ex) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw ex;
    }

    //make any pricing changes - possibly retrying with the persisted state if there's a lock failure
    if (priceOrder) {
        int retryCount = 0;
        boolean isValid = false;
        while (!isValid) {
            try {
                order = pricingService.executePricing(order);
                isValid = true;
            } catch (Exception ex) {
                boolean isValidCause = false;
                Throwable cause = ex;
                while (!isValidCause) {
                    if (cause.getClass().equals(LockAcquisitionException.class)) {
                        isValidCause = true;
                    }
                    cause = cause.getCause();
                    if (cause == null) {
                        break;
                    }
                }
                if (isValidCause) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Problem acquiring lock during pricing call - attempting to price again.");
                    }
                    isValid = false;
                    if (retryCount >= pricingRetryCountForLockFailure) {
                        if (LOG.isInfoEnabled()) {
                            LOG.info("Problem acquiring lock during pricing call. Retry limit exceeded at ("
                                    + retryCount + "). Throwing exception.");
                        }
                        if (ex instanceof PricingException) {
                            throw (PricingException) ex;
                        } else {
                            throw new PricingException(ex);
                        }
                    } else {
                        order = findOrderById(order.getId());
                        retryCount++;
                    }
                    try {
                        Thread.sleep(pricingRetryWaitIntervalForLockFailure);
                    } catch (Throwable e) {
                        //do nothing
                    }
                } else {
                    if (ex instanceof PricingException) {
                        throw (PricingException) ex;
                    } else {
                        throw new PricingException(ex);
                    }
                }
            }
        }

        //make the final save of the priced order
        status = TransactionUtils.createTransaction("saveOrder", TransactionDefinition.PROPAGATION_REQUIRED,
                transactionManager);
        try {
            order = persist(order);

            if (extensionManager != null) {
                extensionManager.getProxy().attachAdditionalDataToOrder(order, priceOrder);
            }
            TransactionUtils.finalizeTransaction(status, transactionManager, false);
        } catch (RuntimeException ex) {
            TransactionUtils.finalizeTransaction(status, transactionManager, true);
            throw ex;
        }
    }

    return order;
}

From source file:org.broadleafcommerce.core.search.service.solr.index.SolrIndexServiceImpl.java

protected void buildIncrementalIndex(int page, int pageSize, SolrIndexOperation operation)
        throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("readItemsToIndex",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with "
                + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }/*from w  ww .j a v a2  s .  c om*/

    try {
        List<? extends Indexable> indexables;
        try {
            operation.beforeReadIndexables();
            indexables = operation.readIndexables(page, pageSize);
        } finally {
            operation.afterReadIndexables();
        }

        try {
            operation.beforeBuildPage();

            operation.buildPage(indexables);
        } finally {
            operation.afterBuildPage();
        }

        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    }

}

From source file:org.broadleafcommerce.core.search.service.solr.index.SolrIndexServiceImpl.java

@Override
public Collection<SolrInputDocument> buildIncrementalIndex(List<? extends Indexable> indexables,
        SolrClient solrServer) throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("executeIncrementalIndex",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with "
                + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }//from w  w w.j  ava  2  s  . co m

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Building incremental product index - pageSize: [%s]...", indexables.size()));
    }

    StopWatch s = new StopWatch();
    try {
        sandBoxHelper.ignoreCloneCache(true);
        extensionManager.getProxy().startBatchEvent(indexables);
        Collection<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
        List<Locale> locales = getAllLocales();

        List<Long> productIds = BLCCollectionUtils.collectList(indexables, new TypedTransformer<Long>() {
            @Override
            public Long transform(Object input) {
                return shs.getCurrentProductId((Indexable) input);
            }
        });

        solrIndexDao.populateProductCatalogStructure(productIds, SolrIndexCachedOperation.getCache());

        List<IndexField> fields = null;
        FieldEntity currentFieldType = null;
        for (Indexable indexable : indexables) {
            if (fields == null || ObjectUtils.notEqual(currentFieldType, indexable.getFieldEntityType())) {
                fields = indexFieldDao.readFieldsByEntityType(indexable.getFieldEntityType());
            }

            SolrInputDocument doc = buildDocument(indexable, fields, locales);
            //If someone overrides the buildDocument method and determines that they don't want a product 
            //indexed, then they can return null. If the document is null it does not get added to 
            //to the index.
            if (doc != null) {
                documents.add(doc);
            }
        }

        extensionManager.getProxy().modifyBuiltDocuments(documents, indexables, fields, locales);

        logDocuments(documents);

        if (!CollectionUtils.isEmpty(documents) && solrServer != null) {
            solrServer.add(documents);
            commit(solrServer);
        }
        TransactionUtils.finalizeTransaction(status, transactionManager, false);

        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Built incremental product index - pageSize: [%s] in [%s]",
                    indexables.size(), s.toLapString()));
        }

        return documents;
    } catch (SolrServerException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (IOException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    } finally {
        extensionManager.getProxy().endBatchEvent(indexables);
        sandBoxHelper.ignoreCloneCache(false);
    }
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrIndexServiceImpl.java

@Override
public void buildIncrementalProductIndex(List<Product> products, boolean useReindexServer)
        throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("executeIncrementalProductIndex",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with "
                + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }//  w  ww.j  a  v  a 2  s.  c  om

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Building incremental product index - pageSize: [%s]...", products.size()));
    }

    StopWatch s = new StopWatch();
    boolean cacheOperationManaged = false;
    try {
        Collection<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
        List<Locale> locales = getAllLocales();

        CatalogStructure cache = SolrIndexCachedOperation.getCache();
        if (cache != null) {
            cacheOperationManaged = true;
        } else {
            cache = new CatalogStructure();
            SolrIndexCachedOperation.setCache(cache);
        }

        List<Field> fields = fieldDao.readAllProductFields();
        List<Long> productIds = BLCCollectionUtils.collectList(products, new TypedTransformer<Long>() {

            @Override
            public Long transform(Object input) {
                return shs.getProductId((Product) input);
            }
        });

        solrIndexDao.populateProductCatalogStructure(productIds, SolrIndexCachedOperation.getCache());

        for (Product product : products) {
            SolrInputDocument doc = buildDocument(product, fields, locales);
            //If someone overrides the buildDocument method and determines that they don't want a product 
            //indexed, then they can return null. If the document is null it does not get added to 
            //to the index.
            if (doc != null) {
                documents.add(doc);
            }
        }

        extensionManager.getProxy().modifyBuiltDocuments(documents, products, fields, locales);

        logDocuments(documents);

        if (!CollectionUtils.isEmpty(documents)) {
            SolrServer server = useReindexServer ? SolrContext.getReindexServer() : SolrContext.getServer();
            server.add(documents);
            commit(server);
        }
        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (SolrServerException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (IOException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    } finally {
        if (!cacheOperationManaged) {
            SolrIndexCachedOperation.clearCache();
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Built incremental product index - pageSize: [%s] in [%s]", products.size(),
                s.toLapString()));
    }
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrIndexServiceImpl.java

@Override
public void buildIncrementalSkuIndex(List<Sku> skus, boolean useReindexServer) throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("executeIncrementalSkuIndex",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with "
                + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }//from  w ww .  ja  v  a  2  s.co m

    StopWatch s = new StopWatch();
    boolean cacheOperationManaged = false;
    try {
        Collection<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
        List<Locale> locales = getAllLocales();

        CatalogStructure cache = SolrIndexCachedOperation.getCache();
        if (cache != null) {
            cacheOperationManaged = true;
        } else {
            cache = new CatalogStructure();
            SolrIndexCachedOperation.setCache(cache);
        }

        List<Field> fields = fieldDao.readAllSkuFields();
        List<Long> productIds = new ArrayList<Long>();
        for (Sku sku : skus) {
            productIds.add(sku.getProduct().getId());
        }

        solrIndexDao.populateProductCatalogStructure(productIds, SolrIndexCachedOperation.getCache());

        for (Sku sku : skus) {
            SolrInputDocument doc = buildDocument(sku, fields, locales);
            //If someone overrides the buildDocument method and determines that they don't want a product 
            //indexed, then they can return null. If the document is null it does not get added to 
            //to the index.
            if (doc != null) {
                documents.add(doc);
            }
        }

        logDocuments(documents);

        if (!CollectionUtils.isEmpty(documents)) {
            SolrServer server = useReindexServer ? SolrContext.getReindexServer() : SolrContext.getServer();
            server.add(documents);
            commit(server);
        }
        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (SolrServerException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (IOException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    } finally {
        if (!cacheOperationManaged) {
            SolrIndexCachedOperation.clearCache();
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Built incremental sku index - pageSize: [%s] in [%s]", skus.size(),
                s.toLapString()));
    }
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrIndexServiceImpl.java

@Override
public void buildIncrementalIndex(int page, int pageSize, boolean useReindexServer) throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("readItemsToIndex",
            TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with "
                + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }//from www .j  a v  a2  s  . c o m

    try {
        if (useSku) {
            List<Sku> skus = readAllActiveSkus(page, pageSize);
            buildIncrementalSkuIndex(skus, useReindexServer);
        } else {
            List<Product> products = readAllActiveProducts(page, pageSize);
            buildIncrementalProductIndex(products, useReindexServer);
        }

        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    }
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.entitymanager.BroadleafEntityManagerInvocationHandler.java

protected Object executeInTransaction(Executable executable, PlatformTransactionManager txManager)
        throws Throwable {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("SandBoxTx");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    Object response;/*from   w ww.j a va 2  s. c o m*/
    TransactionStatus status = txManager.getTransaction(def);
    try {
        response = executable.execute();
    } catch (Throwable ex) {
        txManager.rollback(status);
        throw ex;
    }
    txManager.commit(status);

    return response;
}

From source file:org.collectionspace.services.authorization.spring.SpringAuthorizationProvider.java

TransactionStatus beginTransaction(String name) {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    // explicitly setting the transaction name is something that can only be done programmatically
    def.setName(name);//from w  ww  .  ja va  2s.  c  om
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    return getTxManager().getTransaction(def);
}