Skip to content
Advertisement

Getting old data with JPA

I’m getting old data with JPA, even if I disable the cache. I guess is because the resource is configured to be RESOURCE_LOCAL, but I’m not sure.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="AppPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.myentities.User</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/mydatabase"/>
            <property name="javax.persistence.jdbc.password" value="*****"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="user1"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

My code that is getting old info about the user:

public List<User> findAll(App app) {       
        getEntityManager().getTransaction().begin();        
        Query q = getEntityManager().createQuery("SELECT t1 FROM User t1 WHERE t1.app.idApp=:idApp");
        q.setParameter("idApp", app.getIdApp());
        getEntityManager().flush();
        getEntityManager().getTransaction().commit();
        List resultList = q.getResultList();        
        return resultList;
    }

My entity:

@Entity
@Table(name = "user")
@Cache (
     type=CacheType.NONE
     )
public class User implements Serializable {

// some attributtes

}

Anybody has some idea of what is going on?

UPDATE 1

The begin, flush and commit methods were just acts of desperation! I know it’s not needed.

I forgot to say something important: the test I make is to add a user record direct on database console and then try to see it through my webapp, which is not showing the new user. That is the “old data” I mentioned, it only displays “old users”.

I already tried to put this on persistence.xml and I didn’t see any difference in the results:

<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="None"/>

So is something elseā€¦

Answer

There are a few suggestions posted already such as ensuring the shared cache is off, and to manage back references so that the cache is consistent. These are for specific situations that could be occuring, but you have not provided enough to say what is really happening.

Another that is specific but seems possible based on your getEntityManager() usage, is if you are reusing the EntityManager instance without clearing it. The EntityManager holds a references to all managed entities since the EM is required to return the same instance on subsequent query and find calls to maintain identity.

If this is not done already, will want to clear the EntityManager or obtain a new one at certain points to release the memory and managed entities.

Advertisement