Search in sources :

Example 11 with StreamCapableTransactionalOperationAdapter

use of org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceServiceImpl method initializeEntityManagerCache.

protected void initializeEntityManagerCache() {
    for (Map<String, Map<String, Object>> targetModeMap : targetModeMaps) {
        for (final String targetMode : targetModeMap.keySet()) {
            final Map<String, Object> managerMap = targetModeMap.get(targetMode);
            transUtil.runTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

                @Override
                public void execute() throws Throwable {
                    populateCaches(targetMode, managerMap);
                }
            }, RuntimeException.class, getTransactionManager(managerMap));
        }
    }
}
Also used : StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 12 with StreamCapableTransactionalOperationAdapter

use of org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter in project BroadleafCommerce by BroadleafCommerce.

the class DynamicEntityRemoteService method inspect.

@Override
public PersistenceResponse inspect(final PersistencePackage persistencePackage) throws ServiceException {
    final PersistenceResponse[] response = new PersistenceResponse[1];
    try {
        PlatformTransactionManager transactionManager = identifyTransactionManager(persistencePackage);
        transUtil.runOptionalTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

            @Override
            public void execute() throws Throwable {
                response[0] = nonTransactionalInspect(persistencePackage);
            }
        }, RuntimeException.class, true, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, true, transactionManager);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof ServiceException) {
            throw (ServiceException) e.getCause();
        }
        throw e;
    }
    return response[0];
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 13 with StreamCapableTransactionalOperationAdapter

use of org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter in project BroadleafCommerce by BroadleafCommerce.

the class DynamicEntityRemoteService method remove.

@Override
public PersistenceResponse remove(final PersistencePackage persistencePackage) throws ServiceException {
    final PersistenceResponse[] response = new PersistenceResponse[1];
    try {
        PlatformTransactionManager transactionManager = identifyTransactionManager(persistencePackage);
        transUtil.runTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

            @Override
            public void execute() throws Throwable {
                response[0] = nonTransactionalRemove(persistencePackage);
            }

            @Override
            public boolean shouldRetryOnTransactionLockAcquisitionFailure() {
                return super.shouldRetryOnTransactionLockAcquisitionFailure();
            }
        }, RuntimeException.class, transactionManager);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof ServiceException) {
            throw (ServiceException) e.getCause();
        }
        throw e;
    }
    return response[0];
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 14 with StreamCapableTransactionalOperationAdapter

use of org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter in project BroadleafCommerce by BroadleafCommerce.

the class GenericEntityDaoImpl method getCeilingImplClass.

@Override
public Class<?> getCeilingImplClass(final String className) {
    final Class<?>[] clazz = new Class<?>[1];
    try {
        clazz[0] = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    // em.unwrap requires a transactional entity manager. We'll only take the hit to start a transaction here if one has not already been started.
    transactionUtil.runOptionalTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

        @Override
        public void execute() throws Throwable {
            Class<?>[] entitiesFromCeiling = daoHelper.getAllPolymorphicEntitiesFromCeiling(clazz[0], em.unwrap(Session.class).getSessionFactory(), true, true);
            if (entitiesFromCeiling == null || entitiesFromCeiling.length < 1) {
                clazz[0] = DynamicDaoHelperImpl.getNonProxyImplementationClassIfNecessary(clazz[0]);
                entitiesFromCeiling = daoHelper.getAllPolymorphicEntitiesFromCeiling(clazz[0], em.unwrap(Session.class).getSessionFactory(), true, true);
            }
            if (entitiesFromCeiling == null || entitiesFromCeiling.length < 1) {
                throw new IllegalArgumentException(String.format("Unable to find ceiling implementation for the requested class name (%s)", className));
            }
            clazz[0] = entitiesFromCeiling[entitiesFromCeiling.length - 1];
        }
    }, RuntimeException.class, !TransactionUtils.isTransactionalEntityManager(em));
    return clazz[0];
}
Also used : StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) Session(org.hibernate.Session)

Example 15 with StreamCapableTransactionalOperationAdapter

use of org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter in project BroadleafCommerce by BroadleafCommerce.

the class MultiTenantCopier method copyEntitiesOfType.

/**
 * Subclasses will generally call this method in their {@link #copyEntities(MultiTenantCopyContext)} implementation.
 *
 * @param clazz
 * @param fromSite
 * @param fromCatalog
 * @param context
 * @param <T>
 * @throws ServiceException
 * @throws CloneNotSupportedException
 */
protected <T extends MultiTenantCloneable> void copyEntitiesOfType(final Class<T> clazz, final Site fromSite, final Catalog fromCatalog, final MultiTenantCopyContext context) throws ServiceException, CloneNotSupportedException {
    genericEntityService.flush();
    genericEntityService.clear();
    transUtil.runStreamingTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

        @Override
        public Object[] retrievePage(int startPos, int pageSize) {
            try {
                Object[] temp = new Object[1];
                List<T> results = readAll(clazz, pageSize, startPos, fromSite, fromCatalog);
                temp[0] = results;
                return temp;
            } catch (ServiceException e) {
                throw ExceptionHelper.refineException(e);
            }
        }

        @Override
        public void pagedExecute(Object[] param) throws Throwable {
            try {
                List<T> results = (List<T>) param[0];
                for (T result : results) {
                    persistCopyObjectTree(new CopyOperation<T, CloneNotSupportedException>() {

                        @Override
                        public T execute(T original) throws CloneNotSupportedException {
                            return (T) original.createOrRetrieveCopyInstance(context).getClone();
                        }
                    }, clazz, result, context);
                }
            } finally {
                genericEntityService.clear();
            }
        }

        @Override
        public Long retrieveTotalCount() {
            try {
                return readCount(clazz, fromSite, fromCatalog);
            } catch (ServiceException e) {
                throw ExceptionHelper.refineException(e);
            }
        }

        @Override
        public boolean shouldRetryOnTransactionLockAcquisitionFailure() {
            return true;
        }
    }, RuntimeException.class);
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)16 ServiceException (org.broadleafcommerce.common.exception.ServiceException)6 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)6 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)5 StaticAssetStorage (org.broadleafcommerce.cms.file.domain.StaticAssetStorage)3 IOException (java.io.IOException)2 Blob (java.sql.Blob)2 Map (java.util.Map)2 Session (org.hibernate.Session)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EntityManager (javax.persistence.EntityManager)1 Site (org.broadleafcommerce.common.site.domain.Site)1 OfferCode (org.broadleafcommerce.core.offer.domain.OfferCode)1 OfferImpl (org.broadleafcommerce.core.offer.domain.OfferImpl)1