2016-04-16 102 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