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

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]