Mar 9, 2017

Camel is closing JPA sessions

So I was writing a simple Camel route where I was fetching a JPA-backed entity on one step and then using it on another. While executing that second step, I got this error:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Wait – I already fetched that object in step 1. Why is Hibernate (my JPA provider) complaining about anything? After poking around with it, I figured it out – it is the lazy-loading the OneToMany property on my entity and when it gets to the next step of the route, the entityManager is closed. I could put an eager fetch on the entire entity, but I didn’t want to do that for all the other places my entity is used. I also could have used the id on the entity to re-fetch the entire entity, but that is another database round-trip.

The solution I used was to use a named entity graph, which is stepped through on this post. We use Spring Data-JPA, in which you can put it on the JpaRepository like this:

interface SomeEntityRepository  implements JpaRepository {
 
    @EntityGraph(type = EntityGraph.EntityGraphType.LOAD)
    SomeEntity findById(Long id)
 
   //  other queries}

Using this will only do an eager fetch when you use findById and not when you run findAll or any other queries.

Another method would be to use the ElementCollection annotation instead of the OneToMany property. They are similar, but not the same. So use with some caution.

These JPA problems aren’t limited to Camel, but it seemed to really be an issue in our case. I have a sample project to play with if you are interested.

About the Author

Object Partners profile.
Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]