これが私の設定、最初のpersistence.xmlです:
<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にアクセスするための仲介者として使用するオブジェクトです。
EclipseLink2.4の使用
そしてここに問題があります。スレッド1を作成し、そのDBStoreオブジェクトを取得し、既存のエンティティにいくつかの変更を加えてデータベースにコミットすると、別の同時スレッド(2)で、変更が行われてコミットされる前後に同じエンティティをロードします。 2番目のスレッドは、最初のスレッドによってコミットされた変更を認識しません。私はそれらを見ることができるので、変更がデータベースにあることを知っています。また、2番目のスレッドでエンティティの値をチェックする前にEntityManager.refresh(entity)を呼び出すと、正常に機能します。したがって、ここでの私の推測では、静的であると考えている同じEntityManagerFactoryを使用する場合、EclipseLinkがキャッシュを共有することになっていますが、2つのスレッドは互いにキャッシュを共有していません。
では、私の設定の何が問題になっていますか?