【发布时间】: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:要执行此操作,必须将其写入事务中,或者您的设置必须允许非事务性写入并且不得分离所有非事务性读取。
【问题讨论】: