在Spring服务内部持久化会导致TransactionRequiredException

时间:2014-09-11 22:53:48

标签: java spring jpa openjpa

由于某些原因,在Spring服务结果中持久存在TransactionRequiredException

我不确定我应该为此问题包含多少信息,因此我可以根据需要添加信息。我有一个Spring服务,它扩展了Abstract类以进行事务管理。

如果我在我的方法中保存,我会收到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:要执行此操作,必须在事务中编写,或者您的设置必须允许非事务性写入,并且不得分离所有非事务性读取。

1 个答案:

答案 0 :(得分:0)

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