簡體   English   中英

在新的持久性之后,以前的持久性數據總是重疊

[英]The previous persisted data always being overlapped after the new persisting

假設我在表'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);

你可以分享,

1) XDao定義

2)什么是您的基礎數據存儲

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

4)您是在存儲庫/服務還是tx:annotation-driven上使用顯式@Transactional

基於以上的組合,行為將有所不同,但是我的猜測可能是服務事務邊界與商店的邊界混淆,或者由於@Transactional機制基於代理,因此只有“外部”方法調用通過代理進入將被攔截,並且您的遞歸調用將不會通過您可能為createTransaction方法設置的AOP。

話雖如此,是否不可能做這樣的事情而不是遞歸:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM