openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexandros Karypidis <akary...@yahoo.gr>
Subject Fetch database changes from external sources
Date Wed, 14 May 2008 09:37:49 GMT
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.


Mime
View raw message