Skip to content

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="" xmlns:xsi="" xsi:schemaLocation="">
    <persistence-unit name="AppPU" transaction-type="RESOURCE_LOCAL">
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://"/>
            <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"/>

My code that is getting old info about the user:

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

My entity:

@Table(name = "user")
@Cache (
public class User implements Serializable {

// some attributtes


Anybody has some idea of what is going on?


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ā€¦


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.