EclipseLink caching behavior

View: New views
3 Messages — Rating Filter:   Alert me  

EclipseLink caching behavior

by Syvalta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I noticed there are quite a lot of discussion about EclipseLink caching, but couldn't spot this specific case. It seems that updating an Entity causes the cache to be updated, even the entity is detached. I would not expect that to happen for a detached object. Actually, I would expect the cache to be updated only after a successful transaction commit.

Some example code:

        open(true, true, true);  // Creates EMF, EM and begins TX
        Item i1 = new Item("item-1");
        i1.setValue("val-1");
        // First add item
        em.persist(i1);
        close(true, true, false); // Commits TX, closes EM, but does not close EMF

        // change Item
        i1.setValue("changed");  // Detached now

        open(false, true, true);  // Uses existing EMF, opens EM and begins TX
        Item i2 = getItem("item-1");  // Fetches item using Query by name
        close(true, true, true);

        System.out.println("i1: " + i1.getValue());
        System.out.println("i2: " + i2.getValue() + ", should be val-1");
        return !i1.getValue().equals(i2.getValue());

So, we
1. add an Entity
2. close the EntityManager making the entity detached
3. change the entity
4. create a new EntityManager
5. fetch the same entity by using it's name field (which is not @Id) in a query

Current result: in point 5 we get the changed value, i.e. i2.getValue() returns "changed"
Expected result: in point 5 we get the original value, i.e. i2.getValue() returns "item-1"

My expectation would be the same even we would be getting the entity (in point 5) from the same entityManager, but it surprises me even more that the changed value is returned from a new entityManager.

Any insights? Are my expectations valid? I'm using EclipseLink 1.0.1 in J2SE mode.

Re: EclipseLink caching behavior

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As you have described it i2's value should not be changed.  So you may need to dig deeper to see what is going on, as it is not correct.  Perhaps print the identity of the objects, and determine when the value gets changed.  Perhaps also set logging to finest.

Ensure that you are creating a new EntityManager for i2, not using the old one, the old one is still the same persistence context, and will have any changes made to the objects.

Syvalta wrote:
Hi,

I noticed there are quite a lot of discussion about EclipseLink caching, but couldn't spot this specific case. It seems that updating an Entity causes the cache to be updated, even the entity is detached. I would not expect that to happen for a detached object. Actually, I would expect the cache to be updated only after a successful transaction commit.

Some example code:

        open(true, true, true);  // Creates EMF, EM and begins TX
        Item i1 = new Item("item-1");
        i1.setValue("val-1");
        // First add item
        em.persist(i1);
        close(true, true, false); // Commits TX, closes EM, but does not close EMF

        // change Item
        i1.setValue("changed");  // Detached now

        open(false, true, true);  // Uses existing EMF, opens EM and begins TX
        Item i2 = getItem("item-1");  // Fetches item using Query by name
        close(true, true, true);

        System.out.println("i1: " + i1.getValue());
        System.out.println("i2: " + i2.getValue() + ", should be val-1");
        return !i1.getValue().equals(i2.getValue());

So, we
1. add an Entity
2. close the EntityManager making the entity detached
3. change the entity
4. create a new EntityManager
5. fetch the same entity by using it's name field (which is not @Id) in a query

Current result: in point 5 we get the changed value, i.e. i2.getValue() returns "changed"
Expected result: in point 5 we get the original value, i.e. i2.getValue() returns "item-1"

My expectation would be the same even we would be getting the entity (in point 5) from the same entityManager, but it surprises me even more that the changed value is returned from a new entityManager.

Any insights? Are my expectations valid? I'm using EclipseLink 1.0.1 in J2SE mode.

Re: EclipseLink caching behavior

by Syvalta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, thanks for your prompt answer. This is really weird, I run the test again and now I can't reproduce it. Before writing this post I run the test in the debugger and checked that the test is doing what it is supposed to do.

The oddest thing is that at that stage I got the test to pass by adding eclipselink.cache.shared.default=false to persistence.xml. Now it is working without it. Hmm, there must have been a bug in the test. Sorry for wasting your time.



As you have described it i2's value should not be changed.  So you may need to dig deeper to see what is going on, as it is not correct.  Perhaps print the identity of the objects, and determine when the value gets changed.  Perhaps also set logging to finest.

Ensure that you are creating a new EntityManager for i2, not using the old one, the old one is still the same persistence context, and will have any changes made to the objects.

Syvalta wrote:
Hi,

I noticed there are quite a lot of discussion about EclipseLink caching, but couldn't spot this specific case. It seems that updating an Entity causes the cache to be updated, even the entity is detached. I would not expect that to happen for a detached object. Actually, I would expect the cache to be updated only after a successful transaction commit.

Some example code:

        open(true, true, true);  // Creates EMF, EM and begins TX
        Item i1 = new Item("item-1");
        i1.setValue("val-1");
        // First add item
        em.persist(i1);
        close(true, true, false); // Commits TX, closes EM, but does not close EMF

        // change Item
        i1.setValue("changed");  // Detached now

        open(false, true, true);  // Uses existing EMF, opens EM and begins TX
        Item i2 = getItem("item-1");  // Fetches item using Query by name
        close(true, true, true);

        System.out.println("i1: " + i1.getValue());
        System.out.println("i2: " + i2.getValue() + ", should be val-1");
        return !i1.getValue().equals(i2.getValue());

So, we
1. add an Entity
2. close the EntityManager making the entity detached
3. change the entity
4. create a new EntityManager
5. fetch the same entity by using it's name field (which is not @Id) in a query

Current result: in point 5 we get the changed value, i.e. i2.getValue() returns "changed"
Expected result: in point 5 we get the original value, i.e. i2.getValue() returns "item-1"

My expectation would be the same even we would be getting the entity (in point 5) from the same entityManager, but it surprises me even more that the changed value is returned from a new entityManager.

Any insights? Are my expectations valid? I'm using EclipseLink 1.0.1 in J2SE mode.