2016-04-16 100 views
0

由于EclipseLink中的更改跟踪,我们目前在我们的应用程序中经历了严重的减速。这个问题是自制的,我们不按照它的意图使用JPA。EclipseLink禁用每个查询的更改跟踪

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

// 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); 
    } 
} 

这将是确定使用的EclipseLink API的#1和#2

我宁愿提示。

的EclipseLink 2.4.2

二级缓存:禁用

ChangeTrackingType:递延

回答

0

尝试使用read-only查询提示,它可以作为一个属性被传递给找到或查询,看看this更多关于提示。只读提示应该从共享二级缓存返回实例,该缓存不应该被修改。由于它没有添加到第一级EntityManager缓存中,所以没有提示的任何其他读取都将生成/返回受管实例。

该文档指出这适用于非事务性读取操作,所以我不确定如果EntityManager使用事务连接进行读取,它将如何工作,因为它不会使用共享高速缓存来读取事务。

+0

感谢您的回答。不幸的是,我们不使用二级缓存(会话缓存)。 使用只读提示可防止在步骤#3中对这些实体进行更改 - 因此这也不起作用。 –

+0

在初始查找操作之后,您将不得不考虑复制您的实体 – Chris