【问题标题】:EclipseLink disable change tracking per queryEclipseLink 禁用每个查询的更改跟踪
【发布时间】:2016-04-16 09:23:57
【问题描述】:

由于 EclipseLink 中的更改跟踪,我们目前的应用程序遇到了严重的减速。问题是自制的,我们没有按预期使用 JPA。

我想知道如何获得缓存命中(第一级),但除非满足某些条件,否则不要将这些实体包括在更改跟踪中。

// not real code, but close if you decompose the layers and inline methods
public void foo(Long customerId, boolean changeName, String newName) {

    /*** check customer valid ***/
    // #1 HOW TO discard from change tracking? – Customer won’t get modified!
    Customer customer = entityManager.find(Customer.class, customerId); 

    // some Business Rules
    // #2 They should be auto discarded from change tracking because #1 is also discarded)
    checkSomething(customer.getAddresses());
    checkSomething(customer.getPhoneNumbers ());
    …

    /*** manipulate customer ***/
    // somewhere else in different classes/methods …
    if(changeName) {
        // #3 HOW TO get Cache hit (1st level) - it was read in #1
        // newName should be persisted
        customer = entityManager.find(Customer.class, customerId); 
        customer.setName(newName);
    }
}

#1 和#2 可以使用 EclipseLink API

我更喜欢提示。

EclipseLink 2.4.2

二级缓存:禁用

ChangeTrackingType: DEFERRED

【问题讨论】:

    标签: jpa caching eclipselink change-tracking


    【解决方案1】:

    尝试使用read-only 查询提示,它可以作为属性传递给查找或查询,有关提示的更多信息,请参阅this。只读提示应从共享的二级缓存返回实例,不应修改。由于它没有添加到第一级 EntityManager 缓存中,因此任何其他没有提示的读取都将构建/返回托管实例。

    文档说明这适用于非事务性读取操作,因此我不确定如果 EntityManager 使用事务性连接进行读取,它将如何工作,因为它不会使用共享缓存通过事务进行读取。

    【讨论】:

    • 感谢您的回答。不幸的是,我们不使用二级缓存(会话缓存)。使用只读提示可以防止在第 3 步中对这些实体进行更改 - 所以这也不起作用。
    • 在初始查找操作之后,您必须考虑复制您的实体
    猜你喜欢
    • 2013-03-18
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多