Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Bugs/256277"

m (Bug Analysis Document: 256277: Endless loop on @PrePersist using a nested Persist)
m (Bug Analysis Document: 256277: Endless loop on @PrePersist using a nested Persist)
Line 51: Line 51:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
public class Leaf implements Serializable {
+
public class Cell implements Serializable {
 
     @PrePersist
 
     @PrePersist
     // 256277: test prePersist
+
     // 256277: test prePersist with a nested persist
 
     public void prePersist() {
 
     public void prePersist() {
         setState("0000");
+
         setState("9999");
 +
        List<Leaf> rowsList = null;       
 +
        Query aQuery = null;       
 +
        // get a reference to the entityManager and perform a persist inside this prePersist
 +
        EntityManager entityManager = TestClient.staticEntityManager;
 +
        processInsert(entityManager);
 +
    }
 +
   
 +
    private void processInsert(EntityManager entityManager) {
 +
        // create 2 cells (to avoid performance optimization for 1 entity)  and link them
 +
        //Leaf cell1 = new Leaf();
 +
        Cell cell1 = new Cell();
 +
        Leaf cell2 = new Leaf();
 +
        cell1.setLeft(cell1);
 +
        cell1.setRight(cell1);
 +
        cell2.setLeft(cell2);
 +
        cell2.setRight(cell2);
 +
        try {
 +
            // Cache objects
 +
            //entityManager.getTransaction().begin();
 +
            entityManager.persist(cell1);
 +
            entityManager.persist(cell2);           
 +
            System.out.println("_processInsert() Inserted: " + cell1);           
 +
            // Store objects       
 +
            entityManager.flush();
 +
            //entityManager.getTransaction().commit();
 +
        } catch (Exception e) {
 +
            e.printStackTrace();
 +
        }       
 
     }
 
     }
 
</source>
 
</source>

Revision as of 16:23, 5 January 2009

Bug Analysis Document: 256277: Endless loop on @PrePersist using a nested Persist


Document History

Date Author Version Description & Notes
20080105 Michael O'Brien 1.0 Initial reproduction use cases

Overview

This bug is encountered when a secondary persist is executed inside of a @PrePersist callback method on the Entity being itself persisted. There are two use cases here, one when the secondary entity persist contains its own @PrePersist callback with a persist - causing an infinite loop, the second use case is when the secondary entity persisted does not do a persist in it's own @PrePersist.

Concepts

JPA Specification Notes

Here is what the JPA specification says about performing persists inside the @PrePersist method. Essentially it states that we should not be performing persists inside a @PrePersist callback - however it is the responsibility of the container to handle any that may be done.

JPA 1.0 Specification

  • P58 Section 3.5
    • "In general, portable applications should not invoke EntityManager or Query operations, access other entity instances, or modify relationships in a lifecycle callback method.[19]"
    • "[19] The semantics of such operations may be standardized in a future release of this specification."

JPA 2.0 Specification

  • P82 Section 3.5.1
    • "In general, portable applications should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.[34] A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked."
    • "[34] The semantics of such operations may be standardized in a future release of this specification."


Reproduction

Prerequisites

  • Run tests out of container on an SE persitence unit
  • At least two entities must be persisted to avoid performance optimization code for a single entity
  • Two different entity classes are required
  • Test must commit more than one object so that the non-performance else clause in CommitManager.commitAllObjectsWithChangeSet() is used
  • Test must perform a change on an existing object that is already persisted
  • Set FlushMode
    • entityManager.setFlushMode(FlushModeType.AUTO);
    • Set @PrePersist method callback on Entity that performs its own read query
@Entity
public class Cell implements Serializable {
    @PrePersist
    // 256277: test prePersist with a nested persist
    public void prePersist() {
        setState("9999");
        List<Leaf> rowsList = null;        
        Query aQuery = null;        
        // get a reference to the entityManager and perform a persist inside this prePersist
        EntityManager entityManager = TestClient.staticEntityManager;
        processInsert(entityManager);
    }
 
    private void processInsert(EntityManager entityManager) {
        // create 2 cells (to avoid performance optimization for 1 entity)  and link them
        //Leaf cell1 = new Leaf();
        Cell cell1 = new Cell();
        Leaf cell2 = new Leaf();
        cell1.setLeft(cell1);
        cell1.setRight(cell1);
        cell2.setLeft(cell2);
        cell2.setRight(cell2);
        try {
            // Cache objects
            //entityManager.getTransaction().begin();
            entityManager.persist(cell1);
            entityManager.persist(cell2);            
            System.out.println("_processInsert() Inserted: " + cell1);            
            // Store objects        
            entityManager.flush();
            //entityManager.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }


Use Case 1: Persist other Entity (with its own @PrePersist and persist) inside @PrePersist callback

This use case currently causes an infinite loop on the following line up to build 20090105.

    EntityClassListener(EntityListener).prePersist(DescriptorEvent) line: 412

Use Case 2: Persist other Entity (with its own @PrePersist with a persist) inside @PrePersist callback

Normal functionality with no infinite loop.

Use Case 3: Persist other Entity (without its own @PrePersist) inside @PrePersist

Normal functionality with no infinite loop.

Analysis Constraints

Design / Functionality

Testing

Use Case 1: Output

The following logs and stacktrace illustrate the current infinite loop behavior when performing a @PrePersist inside a @PrePersist as a result of a persist in this callback method.

StackTrace

Logs

API

GUI

Config files

Documentation

Open Issues

Issue # Owner Description / Notes
I1 mobrien Verify that Entity containing the @PrePersist callback has been registered

Decisions

Issue # Description / Notes Decision

Future Considerations

During the research for this bug, the following items were identified as out of scope but are captured here as potential future enhancements. If agreed upon during the review process these should be logged in the bug system.

References

Back to the top