【问题标题】:Persisting inside the Spring service results into TransactionRequiredException在 Spring 服务中持久化导致 TransactionRequiredException
【发布时间】:2014-09-11 22:53:48
【问题描述】:

由于某种原因,在 Spring 服务中持久化导致 TransactionRequiredException

我不确定我应该为这个问题包含多少信息,所以如果需要我可以添加信息。我有一个 Spring 服务,它扩展了抽象类以进行事务管理。

如果我保存在我的方法中,我会得到 org.apache.renamed.openjpa.persistence.TransactionRequiredException

即使我为该方法指定了@Transactional。

通过在 Service 类之外调用服务调用的相同方法可以正常工作。

我的方法:

@Service("PriceListLookupService")
public class PriceListLookupServiceImpl extends
    AbstractEpEntityService<PriceList> implements
    PriceListLookupService {
@Override
@Transactional
public PriceList createPriceListForCatalogStore(String catalogName,
        String storeName) {
    // Catalog catalog = catalogService.findByName(catalogName);
    try {
        PriceList priceList = new PriceList();
        this.saveOrUpdate(priceList);
    } catch (Exception e) {
        LOG.error("createDestinationPriceListError", e);
    }
    return null;
}

抽象类

public abstract class AbstractEntityService<T, I> implements EntityService<T, I> {

@PersistenceContext
protected EntityManager entityManager;
protected Class<T> entityClass;


@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public T saveOrUpdate(T entity) {
    if(isEntityPersistent(entity)) {
        entity = entityManager.merge(entity);
    } else {
        entityManager.persist(entity);
    }
    entityManager.flush();
    return entity;
}

例外:

org.apache.renamed.openjpa.persistence.TransactionRequiredException:要执行此操作,必须将其写入事务中,或者您的设置必须允许非事务性写入并且不得分离所有非事务性读取。

【问题讨论】:

    标签: java spring jpa openjpa


    【解决方案1】:

    看来,服务中的第一个调用方法应该具有@Transactional 注释。因此,如果调用第一个方法 A,该方法在服务中调用方法 B,则方法 A 必须具有 @Transactional 注释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多