Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cache issue
Cache issue [message #483924] Thu, 03 September 2009 15:47 Go to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Dear all,

I have a problem when I enable the eclipselink cache default one).

All my CRUD (Create, Read, Update and Delete) are implemented
instantiating and closing an EntityManager. For example:

public final T findById(I id) {

EntityManager entityManager = JpaManager.getManager();

try {
return (T) entityManager.find(entityClass, id);
}catch(Exception ex){
....
} finally {
entityManager.close();
}
return null;
}


public final void save(final T object) {

EntityManager entityManager = JpaManager.getManager();

try {
entityManager.getTransaction().begin();

entityManager.persist(object);

entityManager.getTransaction().commit();
}catch(Exception ex){
entityManager.getTransaction().rollback();
......
}finally {
entityManager.close();
}
}


It happens that sometimes I save an object and then I load (e.g.
findBtyId) but the retrieved value in not updated (that is, it is not
the last value I have saved or updated). On the DB, the table contain
the right value (the last saved).


I've tried to disable the cache in the persistence.xml file:

<property name="eclipselink.cache.type.default" value="None"/>

and all is going well.

Any help about?I need to configure the cache differently basing on the
structure of my code?)

BR,
Enrico
Re: Cache issue [message #484432 is a reply to message #483924] Mon, 07 September 2009 12:56 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
I would like to add a question to my previous post.
What is the best way or mode to run eclipselink jpa on Tomcat?
Is my previous architecture (instantiate and then close a new
entityManager every time I need to save/update/delete data on my local DB)?

If not, what is the best way?

BR,
Enrico

Enrico ha scritto:
> Dear all,
>
> I have a problem when I enable the eclipselink cache default one).
>
> All my CRUD (Create, Read, Update and Delete) are implemented
> instantiating and closing an EntityManager. For example:
>
> public final T findById(I id) {
>
> EntityManager entityManager = JpaManager.getManager();
>
> try {
> return (T) entityManager.find(entityClass, id);
> }catch(Exception ex){
> ....
> } finally {
> entityManager.close();
> }
> return null;
> }
>
>
> public final void save(final T object) {
>
> EntityManager entityManager = JpaManager.getManager();
>
> try {
> entityManager.getTransaction().begin();
>
> entityManager.persist(object);
>
> entityManager.getTransaction().commit();
> }catch(Exception ex){
> entityManager.getTransaction().rollback();
> ......
> }finally {
> entityManager.close();
> }
> }
>
>
> It happens that sometimes I save an object and then I load (e.g.
> findBtyId) but the retrieved value in not updated (that is, it is not
> the last value I have saved or updated). On the DB, the table contain
> the right value (the last saved).
>
>
> I've tried to disable the cache in the persistence.xml file:
>
> <property name="eclipselink.cache.type.default" value="None"/>
>
> and all is going well.
>
> Any help about?I need to configure the cache differently basing on the
> structure of my code?)
>
> BR,
> Enrico
>
Re: Cache issue [message #484536 is a reply to message #484432] Tue, 08 September 2009 07:25 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
You either have a EM per request (that is what you have now) or you could consider using an EM per session. But you have to be careful with multiple requests trying to start the transaction, so one-per-request is the safest.


Enrico wrote:
> I would like to add a question to my previous post.
> What is the best way or mode to run eclipselink jpa on Tomcat?
> Is my previous architecture (instantiate and then close a new
> entityManager every time I need to save/update/delete data on my local DB)?
>
> If not, what is the best way?
>
> BR,
> Enrico
>
> Enrico ha scritto:
>> Dear all,
>>
>> I have a problem when I enable the eclipselink cache default one).
>>
>> All my CRUD (Create, Read, Update and Delete) are implemented
>> instantiating and closing an EntityManager. For example:
>>
>> public final T findById(I id) {
>> EntityManager entityManager = JpaManager.getManager();
>> try { return (T)
>> entityManager.find(entityClass, id); }catch(Exception ex){
>> ....
>> } finally {
>> entityManager.close();
>> } return null;
>> }
>>
>>
>> public final void save(final T object) {
>>
>> EntityManager entityManager = JpaManager.getManager();
>> try {
>> entityManager.getTransaction().begin();
>>
>> entityManager.persist(object);
>>
>> entityManager.getTransaction().commit();
>> }catch(Exception ex){
>> entityManager.getTransaction().rollback();
>> ......
>> }finally {
>> entityManager.close();
>> }
>> }
>>
>>
>> It happens that sometimes I save an object and then I load (e.g.
>> findBtyId) but the retrieved value in not updated (that is, it is not
>> the last value I have saved or updated). On the DB, the table contain
>> the right value (the last saved).
>>
>>
>> I've tried to disable the cache in the persistence.xml file:
>>
>> <property name="eclipselink.cache.type.default" value="None"/>
>>
>> and all is going well.
>>
>> Any help about?I need to configure the cache differently basing on the
>> structure of my code?)
>>
>> BR,
>> Enrico
>>
Re: Cache issue [message #484627 is a reply to message #483924] Tue, 08 September 2009 14:32 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

That is odd, the cache should always be up to date. It could be an issue with you save() method. It use persist() which in JPA is only valid for saving new objects, if it is an existing object then you must use merge().

To disable to cache do not set the type to none, instead use,

"eclipselink.cache.shared.default"="false"


James : Wiki : Book : Blog : Twitter
Re: Cache issue [message #484745 is a reply to message #484627] Wed, 09 September 2009 06:10 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Thank you James for your reply.
My save method is the following (for new object):

@Override
public final void save(final T object) {

//Get the entity Manager
EntityManager entityManager = JpaManager.getManager();

try {
entityManager.getTransaction().begin();
entityManager.persist(object);

entityManager.getTransaction().commit();
}catch(Exception ex){
//Back to the previous status
entityManager.getTransaction().rollback();
}
finally {
JpaManager.closeManager(entityManager);
}
}



the edit method (to update entity) is:

@Override
public final void edt(T object) {
//Get the entity Manager
EntityManager entityManager = JpaManager.getManager();

try {
entityManager.getTransaction().begin();
object = entityManager.merge(object);

entityManager.getTransaction().commit();
}catch(Exception ex){
//Back to the previous status
entityManager.getTransaction().rollback();
}
finally {
JpaManager.closeManager(entityManager);
}
}


As you can see I use persist() for new object and merge() for existing
object.
Have you an explanation about the fact my code seems to work when I set
the cachen type to None?From documentation:

"NONE – This option does not preserve object identity and does not cache
objects. Oracle does not recommend using this option."

Is this not to disable the cache?

For "eclipselink.cache.shared.default":


"The default for whether or not the EclipseLink session cache is shared
by multiple client sessions."

Why is this to disable the cache?What it is mean with client sessions?I
crete and close my entity manager using the "method way", that is every
time I call a persist, merge etc... function.

BR,
Enrico



James ha scritto:
> That is odd, the cache should always be up to date. It could be an
> issue with you save() method. It use persist() which in JPA is only
> valid for saving new objects, if it is an existing object then you must
> use merge().
>
> To disable to cache do not set the type to none, instead use,
>
> "eclipselink.cache.shared.default"="false"
Re: Cache issue [message #484746 is a reply to message #484536] Wed, 09 September 2009 06:16 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
I think to use EM per method because it is safest.
I would like just to know if this is a good way (also in terms of
performances) to use persistence in webapp, that is I use persistence
with a webapp on Tomcat (using non-JTA JNDI datasource external
connection pool defined in Tomcat).
That is in my persistence.xml:

<non-jta-data-source>java:comp/env/ds/myDS</non-jta-data-source >
.......
<property name="eclipselink.session.customizer" value="......my sessions
customizer class"/>

<property name="eclipselink.target-database" value="...PostgreSQL.."/>



<property name="javax.persistence.nonJtaDataSource"
value="java:comp/env/ds/myDS"/>

Thank you,
Enrico



tbee ha scritto:
> You either have a EM per request (that is what you have now) or you
> could consider using an EM per session. But you have to be careful with
> multiple requests trying to start the transaction, so one-per-request is
> the safest.
>
>
> Enrico wrote:
>> I would like to add a question to my previous post.
>> What is the best way or mode to run eclipselink jpa on Tomcat?
>> Is my previous architecture (instantiate and then close a new
>> entityManager every time I need to save/update/delete data on my local
>> DB)?
>>
>> If not, what is the best way?
>>
>> BR,
>> Enrico
>>
>> Enrico ha scritto:
>>> Dear all,
>>>
>>> I have a problem when I enable the eclipselink cache default one).
>>>
>>> All my CRUD (Create, Read, Update and Delete) are implemented
>>> instantiating and closing an EntityManager. For example:
>>>
>>> public final T findById(I id) {
>>> EntityManager entityManager = JpaManager.getManager();
>>> try { return (T)
>>> entityManager.find(entityClass, id); }catch(Exception ex){
>>> ....
>>> } finally {
>>> entityManager.close();
>>> } return null;
>>> }
>>>
>>>
>>> public final void save(final T object) {
>>>
>>> EntityManager entityManager = JpaManager.getManager();
>>> try {
>>> entityManager.getTransaction().begin();
>>>
>>> entityManager.persist(object);
>>>
>>> entityManager.getTransaction().commit();
>>> }catch(Exception ex){
>>> entityManager.getTransaction().rollback();
>>> ......
>>> }finally {
>>> entityManager.close();
>>> }
>>> }
>>>
>>>
>>> It happens that sometimes I save an object and then I load (e.g.
>>> findBtyId) but the retrieved value in not updated (that is, it is not
>>> the last value I have saved or updated). On the DB, the table contain
>>> the right value (the last saved).
>>>
>>>
>>> I've tried to disable the cache in the persistence.xml file:
>>>
>>> <property name="eclipselink.cache.type.default" value="None"/>
>>>
>>> and all is going well.
>>>
>>> Any help about?I need to configure the cache differently basing on
>>> the structure of my code?)
>>>
>>> BR,
>>> Enrico
>>>
Re: Cache issue [message #484841 is a reply to message #484745] Wed, 09 September 2009 13:37 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I can't see how the cache could be getting out of synch if your database is getting the correct values. Can you isolate when the corruption occurs exactly? If you can perhaps log a bug and attach your test case. The only thing that I can think of is that you call persist/save for an existing object, or you new object has a reference to an existing object, that somehow causes the corruption.

You may also want to try the latest version or build if you have not already done so.

In terms of the doc you referenced, I assume you got that from the User Guide, it does seem confusing, please log a bug for this. The API doc seems more clear,

http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/PersistenceUnitProperties.html#CACHE_SHARED_

http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/CacheType.html


James : Wiki : Book : Blog : Twitter
Re: Cache issue [message #484866 is a reply to message #484841] Wed, 09 September 2009 14:22 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
I've tested the following situations (also to measure some performance
results in terms of response time):

1. "eclipselink.cache.type.default"="None"
It seems work (object is update on DB) but performances are poor;

2. "eclipselink.cache.shared.default"="false"
It seems to work and performance seems to be better than case 2.
(response-time is about an half of the previous one)

3. default eclipselink cach (shared and SoftWeak)
It does not work....on the DB I note the record is updated, but when I
try to read the object I continuosly get the old value. Performance are
higher than previous one (response-time is about an half of the previous
one)

I really would like to have both the better performance but be sure
objects aI read are up to date.
I will try to run some more test, alo because currently the error seems
to happen for a specific entity...maybe it can be some problem due to
relations, LAZY load etc...but I need to investigate in depth.

If you have other ideas please let me know.

BR,
Enrico


James ha scritto:
> I can't see how the cache could be getting out of synch if your database
> is getting the correct values. Can you isolate when the corruption
> occurs exactly? If you can perhaps log a bug and attach your test
> case. The only thing that I can think of is that you call persist/save
> for an existing object, or you new object has a reference to an existing
> object, that somehow causes the corruption.
>
> You may also want to try the latest version or build if you have not
> already done so.
>
> In terms of the doc you referenced, I assume you got that from the User
> Guide, it does seem confusing, please log a bug for this. The API doc
> seems more clear,
>
> http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/PersistenceUnitProperties.html#CACHE_SHARED_
>
>
> http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/CacheType.html
>
>
Re: Cache issue [message #485028 is a reply to message #484866] Thu, 10 September 2009 10:09 Go to previous message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
I conducted some more test.
I think there is some problem using the shared cache, but I don't know
if it is an eclipselink bug or it is my code bug.

Using default cache settings (shared cache enabled), it seems the cached
object is not updated with the DB. The object not updated is access not
directly but through entity relationship (manytomany). Let say I have
object A that is related to object B that is related to object C:

A ----ManyToOne----> B -----ManyToMany----> C

the object that seems not to be updated is C.

If I load the object directly (using the load(id of C) on the entity C)
the object return the right value (stored on DB).
If I access the object starting from the object A and (load(id of A))
and navigate to object C through java object, the value is not updated
to DB.

All works if I set to "false" the shared cache.
Looking the FINEST logging I note that disabling the shared cache, the
load(id of A) call a set of query on the DB to get values. With default
cache otherwise no query have done on the DB but the object in the cache
is returned (but it is not updted to the DB).

Take in mind that I update (save the new value) of the object C not
throught the object A but loading directly the object C. This because I
have different point of my webapp where I can load/update/delete
entities. So waht is happening in my view is the following:

1. I have my webapp on
2. from a point of code I load the object A and read value of object C
using object relationship
3. from another point of code (another thread...e.g. another user in my
webapp) I load directly object C (I mean the sme record on the DB), edit
it and save new value
4. Now if the user repeat the point 2, he see tha old value and not the
update one...because the object A is in the cache and the related object
B and C are also in the cache and they are not updated to the DB.

So, is it possible that the error i due to I have more entity instances
of the same DB record?and one or that instance is changed and saved
(merged) to the DB, the other instances are not updated?
If so, how can I solve this problem if I would like to use the default
shared cache to get better performances?

BR,
Enrico




Enrico ha scritto:
> I've tested the following situations (also to measure some performance
> results in terms of response time):
>
> 1. "eclipselink.cache.type.default"="None"
> It seems work (object is update on DB) but performances are poor;
>
> 2. "eclipselink.cache.shared.default"="false"
> It seems to work and performance seems to be better than case 2.
> (response-time is about an half of the previous one)
>
> 3. default eclipselink cach (shared and SoftWeak)
> It does not work....on the DB I note the record is updated, but when
> I try to read the object I continuosly get the old value. Performance
> are higher than previous one (response-time is about an half of the
> previous one)
>
> I really would like to have both the better performance but be sure
> objects aI read are up to date.
> I will try to run some more test, alo because currently the error seems
> to happen for a specific entity...maybe it can be some problem due to
> relations, LAZY load etc...but I need to investigate in depth.
>
> If you have other ideas please let me know.
>
> BR,
> Enrico
>
>
> James ha scritto:
>> I can't see how the cache could be getting out of synch if your
>> database is getting the correct values. Can you isolate when the
>> corruption occurs exactly? If you can perhaps log a bug and attach
>> your test case. The only thing that I can think of is that you call
>> persist/save for an existing object, or you new object has a reference
>> to an existing object, that somehow causes the corruption.
>>
>> You may also want to try the latest version or build if you have not
>> already done so.
>>
>> In terms of the doc you referenced, I assume you got that from the
>> User Guide, it does seem confusing, please log a bug for this. The
>> API doc seems more clear,
>>
>> http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/PersistenceUnitProperties.html#CACHE_SHARED_
>>
>>
>> http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/CacheType.html
>>
>>
Previous Topic:Is TABLE_PER_CLASS supposed to work with eclipselink-2.0.0.v20090731-r4765 (m6)
Next Topic:TEST POLL: What database do you use?
Goto Forum:
  


Current Time: Tue Apr 16 14:00:43 GMT 2024

Powered by FUDForum. Page generated in 0.36279 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top