java web application caching data, how to stop it so that the data is not stale!

1.1k views Asked by At

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]

2

There are 2 answers

5
developer On

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.

2
Will Hartung On

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.