Jakarta ee Eclipselink线程间共享缓存

Jakarta ee Eclipselink线程间共享缓存,jakarta-ee,jpa,jpa-2.0,eclipselink,Jakarta Ee,Jpa,Jpa 2.0,Eclipselink,以下是我的配置,first 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/persiste

以下是我的配置,first 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>
其中DBStore是我用作访问EntityManager的中间人的对象

使用EclipseLink 2.4

这就是问题所在。如果我创建线程1,为它获取一个DBStore对象,对一个现有实体进行一些更改,将它们提交到数据库,并且我让另一个并发线程(2)在进行更改和提交更改前后加载同一实体,那么第二个线程看不到第一个线程提交的更改。我知道更改在数据库中,因为我可以看到它们。另外,若在第二个线程上,我在检查实体的值之前调用EntityManager.refresh(实体),那个么它可以正常工作。因此,我的猜测是,我的两个线程没有彼此共享缓存,即使EclipseLink应该这样做,如果您使用相同的EntityManagerFactory,我认为它是静态的


那么,我的设置有什么问题

每个EntityManager都有自己的缓存,因为它们表示不同的事务上下文。因此,读取到EntityManager中的现有实体不会显示来自其他实体的更改,除非刷新或清除EM并重新读取实体。Eclipselink缓存关于JPA的描述如下:

你说得对,我之前读到“本地EntityManager缓存未共享”时完全错过了这一部分,“我想知道是否有办法阻止EntityManager进行内部缓存而只使用共享缓存。我想另一个选择是,我不需要太长时间地坚持实体经理。
private static EntityManagerFactory factory = null;

public synchronized static DBStore getInstance() {
    if (factory == null) {
        factory = Persistence.createEntityManagerFactory("db");
    }
    return new DBStore(factory.createEntityManager());
}