Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
Asked Answered
Y

4

56

I have been suffering from infamous hibernate exception

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

Now the community is cheering over

<property name="hibernate.enable_lazy_load_no_trans" value="true"/>

saying it solves the problem but USE IT WITH CAUTION.

What they mean by use it with caution? What this property actually does?

Please give me any insights.

Yawn answered 18/8, 2014 at 12:1 Comment(2)
@SteveChambers 404 - dead linkFunnyman
Please find my answer: https://mcmap.net/q/103141/-how-hibernate-initialize-works Hope this helpsYenta
C
47

The problem with this approach is that you can have the N+1 effect.

Imagine that you have the following entity:

public class Person{
    @OneToMany // default to lazy
    private List<Order> orderList;
}

If you have a report that returns 10K of persons, and if in this report you execute the code person.getOrderList() the JPA/Hibernate will execute 10K of queries. This is the N+1 effect, you will have no control about all the queries that will be executed.

Imagine now that Order is like below:

public class Order{
    @OneToMany // default to lazy
    private List<EmailSent> emailSentList;
}

Imagine now that you have a iteration with the person.getOrderList() and for every Order orderyou will do a order.getEmailSentList(). Can you see the problem now?

For LazyInitializationException you can have some solutions:

  • Use the OpenInSessionInView approach. You will need to create a WebFilter that will open and close the transaction. The problem with is the N+1 effect.
  • Use the hibernate.enable_lazy_load_no_trans configuration, that is a hibernate and you will not be able to port your project to other JPA provider if needed. You also can have the N+1 effect.
  • Use the EJB feature named PersistenceContext Extended. With this you will keep the context opened of several transactions. The problems are: N+1 effect can happen, use a lot of server memory (entities will stay managed)
  • Use the FETCH in the query. With this approach you could do a JPQL/HQL like: select p from Person p join fetch p.orderList. With this query you will have your list loaded from the database and will not have the N+1 effect. The problem is that you will need to write a JPQL for each case.

If you still have any problem, check these links:

Cabalistic answered 18/8, 2014 at 16:32 Comment(9)
OpenSessionInView will never have this "N+1 effect", that would'nt make any sense. Only ONE session will be opened and every entity-lookup within that will be handled normally - most likely via JOINs, depending on your fields. You have obviously not understood how the open-session-in-view method works. The one and only problem with it is : in bad situations (and with bad coding) your user may not get any information if your transaction actually got rolled back or even failed, dending on your transaction-configuration.Interdental
N+1 is not about several sessions, but is about several trips do the database.Cabalistic
in this case these are the same thing - during a view-created session all of your entity attributes may or may not be fetched with a single "trip" - depending on your ORM-implementation and/or fetch configuration.Interdental
I am not sure if you understood what I told up there. With open session in view you will have the N+1 effect, you will keep the transaction opened and for each get of a non fetched entity a new trip to the database will be done. I said about n+1 and fetch in a jpql, what is your point?Cabalistic
yes, you now discovered the primary aspect of lazy initialization. I recommend going deeper into the topic; such as the difference to eager-fetch, cardinalities and especially the fact that your "problem" is omnipresent and a JOIN FETCH will simply remove the DBMS-driver overhead for a few additional "trips" but also decrease the performance in large data-sets. JOINs may work for small data-sets exclusively. Hybrid approaches may also yield acceptable results.Interdental
I like this conversation, but both sides are so confident and conflicting. Can someone provide a reliable resource that illustrates the tradeoffs between lazy and eager? Also, let's address the fact that it's silly I can even choose @Lazy when there's another property I just "need to set" otherwise it doesn't work.Gluttony
@Cabalistic , so do you recommend Use the FETCH in the query as best approach?Portauprince
Great answer. Would really appreciate an example though. I have tried a join query with @Basic(fetch=FetchType.EAGER) along with @Transactional(propagation=Propagation.Requires_new), (among hundreds of other attempts) but so far only the property has worked for me. The property works like a charm, but I’d rather not compromise my dozens of other entity classes in exchange.Marron
Join Fetch is not useful! We have a many to one relation, we need to use some conditions on the join means something like this: LEFT JOIN e.clientDatasource cd ON cd.isActive = TRUE AND cd.isDeleted = FALSE and Fetch does not work with it, if we use LEFT JOIN FETCH it will throw an exception saying fetch can not accompany with join condition!. We can not have these two conditions on the query because the cd might be null and then it will not retrieve the e as well.Magpie
F
11

This goes against how we can take advantage of Hibernate's enforcement of repeatable read semantics with the Session concept. When an object is first loaded and if the object is referenced again within the life of the session, then the same object is returned IRRESPECTIVE of whether this object has changed in the DB. This is the repeatable read semantics provided automatically by hibernate.

With this setting, you have no session providing this guarantee, so if you now access this data you will be getting the latest version of the data.

This might be fine. But consider the scenario where this object is held in some place for a long time and the data has changed considerably, so that the lazily fetched data is much different that the data already loaded when the session was alive. This is what you need to be concerned about.

To put it simple you can safely use this setting if your program is not affected by: How stale the data that was already fetched when in session to the data that will be fetched lazily out of session

But if this (your program is exposed to timing issues, when it might work fine one time and fail another time) is a concern, then fetch all the necessary data while in session.

Feola answered 25/5, 2015 at 19:25 Comment(0)
B
6

The best way to solve the LazyInitializationException is to use the JOIN FETCH directive in your entity queries.

EAGER loading is bad for performance. Also, there are anti-patterns such as:

Which you should never use since they either require the database connection to be open for the UI rendering (Open Session in View), or a database connection is needed for every lazy association that is fetched outside of the initial Persistence Context (hibernate.enable_lazy_load_no_trans).

Sometimes, you don't even need entities, and a DTO projection is even better.

Bakelite answered 27/9, 2016 at 11:10 Comment(10)
please explain the last sentence "don't even need entities, and a DTO projection is even better.". What is a DTO projection? and why it can replace entities.Lilly
and what do you mean by "anti-patterns"?Lilly
Anti-Pattern means that it's disguised as a solution while, in reality, it's a poor solution to a problem.Bakelite
I read your blog (the reference in your post) and also tried, if Post object in PostComment is empty, then the result would be wrong: when postComment exists, but it does not have Post, it should return a PostComment with empty/null Post, but it actually returns no PostComment.Lilly
What to do if you use spring boot... like findById method?Agosto
If you use Spring Boot, it means you use Spring Data, so you can use a custom Repository method to execute the proper JPQLBakelite
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true is used in springbootPhysiological
By default, it's not used. If you enabled it explicitly, then you should consider removing this antipattern.Bakelite
@VladMihalcea as your comments and articles?Should we, dont use hibernate assocations(onetomany,manytoone..etc) for eager/lazy loading .Should we use inner joins with custom dtos,And we doesnt prefer lazy and eager loading both for performance issue?Should we?Menis
@VladMihalcea you have a lot articles and books and they are are very valuable for us,thanks for your sharing.For that question if it is possible just quick comment yes or no about hibernate assocations,for your DTO article ,as I understand we shouldnt use lazy loading?Y/N.Menis
G
3

Probably because there are better solutions, like @Transactional, where opening and closing sessions follows a very common pattern of "open a session then wrap everything in a try-catch-finally; catch rolls back and finally closes the session." This annotation is typically at the request-level for web apps and services.

Or if you need more granular control you can open sessions manually using a SessionFactory.

And as others have mentioned, lazy-loading is something you need to be aware of. It's not a silver bullet but it can be very helpful. Generally, if your apps are designed to have many small requests then its ok.

Eager loading can also be very bad. For example, when your object model has lots of many-to-many relationships but your requests don't use data more than one level deep.

Or you can just forget the whole thing for now. Use lazy loading until it becomes an issue. And if it does, you would have been better of with Mybatis anyway.

Gluttony answered 21/5, 2015 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.