Open JPA - L2 Cache Issue and Workaround
Join the DZone community and get the full member experience.
Join For FreeI ran into an interesting issue with OpenJPA in one of my projects. I thought it would be good to share the issue I faced and the workaround I found for benefit of anyone who might run into the same issue.
The issue comes up when we are trying to load an entity that has some relations with fetch type set to LAZY. This issue is observed only when L2 cache is turned ON. L2 cache is typically turned ON to improve performance and can be done by adding following property to persistence unit in persistence.xml:
<property name="openjpa.DataCache" value="true" />
To explain the issue in detail I will use a model so that we can base our discussion around it. So first lets get that down:
@Entity public class Person { @Id @GeneratedValue(generator = "person_seq", strategy = GenerationType.SEQUENCE) private int id; private String firstName; private String lastName; @OneToOne(mappedBy = "person", fetch = FetchType.LAZY) private Address address; @OneToMany(cascade = CascadeType.ALL, mappedBy = "person", orphanRemoval=true) private List<PhoneNumber> phoneNumbers; .... } @Entity public class Address { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String streetAddress; private String city; private String zipCode; @OneToOne @JoinColumn(name="PERSON_ID") private Person person; ... } @Entity public class PhoneNumber { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String number; @OneToOne @JoinColumn(name="PERSON_ID") private Person person; ... }
Problem
This issue was observed with OpenJPA 2.2.2. Looking up online revealed that there was a defect that was fixed on trunk that was related to L2 cache (https://issues.apache.org/jira/browse/OPENJPA-2285), which appeared to be the one that would solve this issue. However, even after back porting that fix in 2.2.2 branch same issue was seen.
Workaround
// Here emf is EntityManagerFactory emf.getCache().evict(Person.class, this.personId);
This workaround ensures that you can use l2 cache (and gain the performance improvements it brings) while making sure you won't run into any unexpected NullPointerExceptions when trying to access lazily loaded children/relations of an entity. If possible use this workaround in your application until a fix is available from OpenJPA to load the entity appropriately.
I have logged a defect against OpenJPA (https://issues.apache.org/jira/browse/OPENJPA-2522) to track this issue and have also submitted a test case to the community.
Published at DZone with permission of Atul Kshirsagar. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
-
Managing Data Residency, the Demo
-
Micro Frontends on Monorepo With Remote State Management
-
Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
Comments