10

I have 2 physical servers which my web application hits managed by load balancers. I always get -

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

when one of the servers are hit while the other one runs smoothly without any problems. I have a local managed cache store enabled and managed by the application. This exception happens only while trying to access one particular column from one table. The rest of the operations work absolutely fine regardless of which server is hit.

Making lazy=false will become a performance issue because the number of rows on that table is quite large. And by the way, we are using get(object) on session instead of load(object).

4 Answers 4

15

From the tags you provided, I deduce you ran into this problem using Spring Framework. I ran into the same LazyInitializationException while using a Spring Data org.springframework.data.jpa.repository.JpaRepository.

I solved the problem by annotating the method which indirectly uses Hibernate to retrieve data from the database with @Transactional.

Sign up to request clarification or add additional context in comments.

Comments

12

It sounds like the column you are trying to access is configured as an association of some sort in your entity (OneToMany, ManyToOne, whatever) and you are not populating that association in your DAO. Then, when you try to access that column (in a location in your code where there is no Hibernate Session), its not populated, Hibernate tries to load it, and boom.

Since you are in fact using the data in that association, making it EAGER sounds like something you actually would want to do. And if that table is so large, you should look at indexing it so that queries against it are efficient.

2 Comments

I have a similar problem but when I use the eager fetching type I run into a different problem: hibernate tries to add my object to a PersistentSet before the field used to implement hashcode/equals are initialized. In my code I have an exception being thrown if that happens so it fails miserably (the hashcode method would be violated otherwise). Any idea how to fix it?
Having the some issue. You say 'and you are not populating that association in your DAO.' how exactly is that achieved?
5

Your object is detached. You need to re-attach it to the current session before accessing it:

session.update(object);

Also make sure you access it within a transaction

Read more about the problem/solution here

Comments

0

Is that an association or property - if it is a property then the issue might be that one of the server's is running a non instrumented version.

Lazy attribute fetching: an attribute or single valued association is fetched when the instance variable is accessed. This approach requires buildtime bytecode instrumentation and is rarely necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.