【问题标题】:The previous persisted data always being overlapped after the new persisting以前的持久化数据在新的持久化之后总是重叠
【发布时间】:2015-08-23 15:20:16
【问题描述】:

假设我在表“X”上有这些数据:

ID    UpdatedDate    ParentID
001   2015-01-01     1
002                  1
003                  1

代码是(我简化了代码):

... 

@Autowired
private XDao xDao;

private void createTransaction(ParentData parentData) {
    List<XData> xDataList = xDao.getDataByParentIdAndUpdatedDateIsNull(parentData.getId());
    XData xData = null;

    // Then we get only the first row of the list
    if(xDataList != null && xDataList.size() > 0) {
        xData = xDataList.get(0);
    } else {
        return;
    }

    // Another transaction
    ...

    // Then we update the UpdatedDate
    xData.setUpdatedDate(new Date());
    xDao.saveAndFlush(xData);

    // And we call the createTransaction again until there is no more xData with a null UpdatedDate on a Parent ID
    createTransaction(parentData);
} 

但是我得到的是一个永无止境的过程,当我检查数据库时,它总是在同一个父 ID 上相互影响数据。所以数据库是这样的:

第一次迭代:

ID    UpdatedDate    ParentID
001   2015-01-01     1
002   2015-02-02     1
003                  1

第二次:

ID    UpdatedDate    ParentID
001   2015-01-01     1
002                  1
003   2015-02-02     1

第三个:

ID    UpdatedDate    ParentID
001   2015-01-01     1
002   2015-02-02     1
003                  1

等等。怎么了?


这是 getDataByParentIdAndUpdatedDateIsNull 类(我简化了代码):

...

public static final String GET_DATA_BY_PARENTIDANDUPDATEDDATEISNULL = 
    "SELECT o FROM XData o " +
    "WHERE o.parentData.parentId = ?1 " +
    "      AND o.updatedDate IS NULL";

@Query(GET_DATA_BY_PARENTIDANDUPDATEDDATEISNULL)
public List<XData> getDataByParentIdAndUpdatedDateIsNull(Long parentId);

【问题讨论】:

  • 你能提供xDao.getDataByParentIdAndUpdatedDateIsNull的代码吗?
  • @mrjimoy_05,你能分享你在这个问题上的发现吗?

标签: java jpa spring-data-jpa overlap transactional


【解决方案1】:

可以分享一下吗

1)XDao的定义

2) 你的底层数据存储是什么

3) 你是否使用任何 ORM 框架

4) 您是在您的存储库/服务上使用显式@Transactional 还是tx:annotation-driven

根据上述组合,行为会有所不同,但我的猜测可能是服务事务边界与 store 的边界混淆,或者由于@Transactional 机制基于代理,因此只有“外部”方法调用到来in 通过代理将被拦截,您的递归调用将不会通过您可能为 createTransaction 方法设置的相同 AOP。

话虽如此,难道不能做这样的事情,而不是递归:

for (XData xData : xDataList) {
    xData.setUpdatedDate(new Date());
    xData.saveAndFlush(xData);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2017-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多