openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig L Russell <Craig.Russ...@Sun.COM>
Subject Re: Fetch database changes from external sources
Date Wed, 14 May 2008 21:00:16 GMT
Hi,

It sounds like you're using the second-level cache which is dutifully  
returning the current contents that are cached.

Have you tried using the evict methods of the second level cache, or  
disabling it for the application?

Craig

On May 14, 2008, at 2:37 AM, Alexandros Karypidis wrote:

> Hi,
>
> First of all, I've done research through the archives for the  
> situation I am facing and found this:
>
> http://mail-archives.apache.org/mod_mbox/openjpa-users/200706.mbox/%3C7262f25e0706152022u4942a14dkec98e30b520350b7@mail.gmail.com%3E
>
> I am facing the exact problem mentioned in that thread: I need to  
> fetch an object several times from the database, whose columns are  
> updated from another program and pick up the changes made by that  
> program. However, I can't get OpenJPA to re-execute SQL and retrieve  
> the object's new data from the database.
>
> In the thread of the link I provide, I was lead to believe that  
> em.refresh(obj) should do the trick. However, OpenJPA does not  
> execute any SQL (I have SQL=TRACE in the log options and I only see  
> one select statement) and keeps the old data in the object.
>
> I use OpenJPA release 1.0.2 and the code snippet with which I test  
> is the following:
>
> =========================================================
> EntityManagerFactory emFactory =
> 	Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
> EntityManager em = emFactory.createEntityManager();
>
> Record r = em.find(Record.class, 1);
> System.out.println("VALUE: " + r.getStrField());
>
> System.out.print("PRESS ENTER TO REDISPLAY...");
> BufferedReader stdin = new BufferedReader(new InputStreamReader(
> 		System.in));
> stdin.readLine();
>
> // ------> PROBLEM AREA <------
> em.refresh(r); // does not work
> // ((OpenJPAEntityManager)em).evict(r); // also does not work
> // em.clear(); // works but detaches everything
> // em.close(); // ... re-create. works, costs too much
> // ------> PROBLEM AREA <------
>
> r = em.find(Record.class, 1);
> System.out.println("VALUE: " + r.getStrField());
> =========================================================
>
> I change the strField of the "Record" object using a database editor  
> before I press enter to re-fetch the value.
>
> So, my question is, why does OpenJPA ignore the call to refresh() if  
> the object is still attached? I don't see it as reasonable to detach  
> all objects just to pick up a change in the database. I figured it  
> may be a transaction isolation issue, but then did the following  
> test which also did not work:
>
> =========================================================
> EntityManagerFactory emFactory =
> 	Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
> EntityManager em = emFactory.createEntityManager();
>
> em.getTransaction().begin();
> Record r = em.find(Record.class, 1);
> System.out.println("VALUE: " + r.getStrField());
> em.getTransaction().commit();
>
> System.out.print("PRESS ENTER TO REDISPLAY...");
> BufferedReader stdin = new BufferedReader(new InputStreamReader(
> 		System.in));
> stdin.readLine();
>
> em.getTransaction().begin();
> r = em.find(Record.class, 1);
> em.refresh(r);
> em.getTransaction().commit();
> System.out.println("VALUE: " + r.getStrField());
> =========================================================
>
> So even when reading the object through 2 different transactions, I  
> still can't get its data to be re-fetched...
>
> Funilly enough, if i call rollback() on the transaction, it does  
> work! However, it seems too much of a kludge to base my solution on  
> that...
>
> Thanks in advace to anyone who takes the time to help out.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Mime
View raw message