What are the main problems when using lazy boot in sleep mode?

I have heard several times that there are problems in sleep mode (especially when using lazy loading). Which ones are the most common and what can be done with them?

+4
source share
3 answers

The most common, probably, is the n + 1 select problem, when lazy loading of the collection causes the database to be hit with n + 1 separate queries instead of a single connection request.

Common sense is the antidote to such questions :-) I believe that all relevant sources (primarily the Hibernate reference ) discuss in detail (and other related) issues along with permissions and workarounds. In short, you should not blindly copy recipes from a cookbook - measure the performance of your code and tune it accordingly. If you see too many releases, you can selectively switch from lazy loading to join or to query the retrieval strategy for this particular property / class / request. (Note that both have their own potential flaws, so again, measuring performance is key.)

Another problem occurs when the client code depends on the actual type of the object / property (for example, by testing it with instanceof . Such code is interrupted if it encounters a proxy object that is not an instance of the specific class that it denotes. However, this does not The best idea is to write such code anyway, and it is very rarely necessary, but sometimes it is inherited by legacy code, which causes a conflict that can make it difficult to work around.

+9
source

First of all, EAGER sampling is a much bigger problem . Lazy choice is the way to go, as it allows you to get as much information as you need.

The only problem you may encounter is a LazyInitializationException if you don't initialize lazy associations while Session open and you try to switch to an uninitialized proxy / collection after closing the Save Context.

The problem with the N + 1 query can occur both for those who wish (when you execute a JPQL query that does not explicitly extract all impatient associations), and lazy associations, and the solution is the same as with LazyInitializationException .

However, you can automatically detect all problems with the N + 1 query during testing. Check out this datasource-proxy utility for more details on this .

+1
source

The HQL sampling strategy can be used to intentionally indicate what needs to be loaded. For example (from the Hibernate Reference ):

from Cat as cat inner join fetch cat.mate left join fetch cat.kittens

Unfortunately, Hibernate does not support all the standard SQL options as part of HQL, which may be prohibitive depending on the requirements of the project. For example, selection is not possible, but is often required for reporting or data analysis.

This can be overcome with the help of Hibernate's ability to execute SQL. However, this approach does not provide an object-oriented HQL Q factor (for example, all connections must be created manually).

0
source

Source: https://habr.com/ru/post/1335834/


All Articles