2

HI, I am having a problem. My (i am guessing) persistence layer is caching my results, so when i update the database from outside my application then the data is remaining stale.

however, it is only caching half the time, i hope this makes sense,

I have a class

@Entity
@Table(name = "PROVINCE")
public class Province implements Serializable {

@Id
@GeneratedValue
int id;

String provinceName;

String provinceMoto;


Buildings buildings;


Units units;


Person person;

.... etc

now, if i update any data that isn't an a reference to a class object, like the 'provincename' then the data is fine in my application and updates straight away. however, the inner objects like 'buildings' doesn't and i cannot figure out how to, if i do a hard refresh like redeploying my app, then the data is fresh.

i get my province from the database here:

Query query = manager.createQuery("select p from Province p where p.person = :query");
        query.setHint("toplink.refresh", "true");
        query.setParameter("query", p);

        province = (Province) query.getSingleResult();

so how do i go about forcing that to also make the inner objects of that class, i.e. the 'buildings' 'person' to also update.

my persistence layer is toplink essentials, and i fixed my earlier problem which was no data updating. and that first post here(it's another stack overflow page)

thank yous for any helps. i hope i explained my problem well enough

[EDIT: like if i wasnt using this framework, it'd just be easier to join the tables in the db and repopulate the data in a new variable, but im not]

Community
  • 1
  • 1
David
  • 893
  • 3
  • 10
  • 15

2 Answers2

2

This is a "Doc it hurts when I..." "Don't do that" question.

If you're updating the database behind the back of Toplink, then you have to refresh your data just like you did.

Every time you use a "select" type query in JPA, it WILL hit the database. But, as you've learned, for related objects, it will simply pull their keys from the DB, and then use the cached version in preference to hitting the database.

Toplink has 2 caches. The Level 1 and Level 2 cache. The Level 1 cache is a transaction based cache use for the current session. The Level 2 cache is similar, but application wide and global in scope. The Level 2 cache is what you are most likely bumping in to.

So you will either need to continue using the Toplink refresh hint as you are doing, or disable the Level 2 Cache completely.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • hi cheers yes, i've read something like that since trying to fix this. i thought that doing this... ...in my persistence unit would fix it but it doesn't, it then complains about index references and stuff in the db – David Mar 22 '11 at 06:03
  • I don't know specifically how to disable it. But you also may want to consider Eclipselink, which is the latest version of TopLink essentials. – Will Hartung Mar 22 '11 at 06:24
0

After you update the data to DB, make your value object (also child value objects) or form reintialise with defeault values. Hope this will slove your problem.

developer
  • 9,116
  • 29
  • 91
  • 150
  • hi, um could you please explain that to me in a lil bit more detail. – David Mar 22 '11 at 05:43
  • what i'm thinking your meaning is that 're-save' the data. so that the new values are cached???? but i'm updating the values/DB from different applications/servers so the new data has to be reinitialised in this application.... – David Mar 22 '11 at 05:45
  • Lets take an example, first you update the data into DB. after you updating data. you are landing on the same form r different form. If it is same form, reinitialise the form so that , it will set all values of ur value object to default values. – developer Mar 22 '11 at 06:07
  • i'm not landing on any form after any updating. it's a completely different running application modifying the DB. now this app i want to look at and hit the DB to get the new data everytime it reloads a form. it just isn't wanting to atm. – David Mar 22 '11 at 06:22
  • sorry, ok my main application(app1) as above, needs to access the database. now that database is being modified by a different application(app2) also. the problem is that the app1 is caching data that is stale, so no matter where or which web page is accessed, the data that the app is caching isn't being updated to the new data. Now that query that is written above in the discription i was expecting to solve this. but it doesn't. how do you propose to invalidate, or get rid of the current data so that this query works. hope that is worded better – David Mar 22 '11 at 06:44