之間共享緩存這裏是我的配置,第一persistence.xml中:的EclipseLink線程
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="db" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- This is needed to allow it to find entities by annotations -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://ip/dbname"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.cache.type.default" value="SoftWeak"/>
</properties>
</persistence-unit>
</persistence>
這是我如何創建我的EntityManagerFactory。
private static EntityManagerFactory factory = null;
public synchronized static DBStore getInstance() {
if (factory == null) {
factory = Persistence.createEntityManagerFactory("db");
}
return new DBStore(factory.createEntityManager());
}
其中DBStore是我用作中間人訪問EntityManager的對象。
使用EclipseLink 2.4
這就是問題所在。如果我創建線程1,爲其獲取DBStore對象,對現有實體進行一些更改,將它們提交到數據庫,並且在創建和提交更改之前和之後,還有另一個併發線程(2)加載同一實體,第二個線程看不到第一個線程提交的更改。我知道這些更改是在數據庫中,因爲我可以看到它們。另外,如果在第二個線程上檢查實體的值之前調用EntityManager.refresh(entity),那麼它工作正常。所以我的猜測是,我的兩個線程並沒有相互共享緩存,即使EclipseLink應該這樣做,如果你使用相同的EntityManagerFactory,我認爲它是靜態的。
那麼我的設置有什麼問題?
你是對的,我在閱讀前面的內容時完全錯過了這個部分'本地EntityManager緩存不是共享的,只存在於EntityManager或事務期間。想知道是否有辦法阻止EntityManagers進行內部緩存並僅使用共享緩存。我想另外一種選擇是,我只是不堅持實體經理太久。 – casolorz