Questions tagged [lazy-initialization]

Lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

This is typically accomplished by maintaining a flag indicating whether the process has taken place. Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot.

In case of Hibernate, the lazy initialization is made in the proxy (created in place of collection) using the hibernate session from which the object was obtained. If the collection is accessed when the original session is closed (for example, outside a @Transactional scope), the LazyInitializationException is thrown.

To initialize the collection, it is enough to call size() or iterate through all elements.

705 questions
69
votes
13 answers

How to implement thread-safe lazy initialization?

What are some recommended approaches to achieving thread-safe lazy initialization? For instance, // Not thread-safe public Foo getInstance(){ if(INSTANCE == null){ INSTANCE = new Foo(); } return INSTANCE; }
mre
  • 43,520
  • 33
  • 120
  • 170
67
votes
9 answers

Does .net core dependency injection support Lazy

I am trying to use the generic Lazy class to instantiate a costly class with .net core dependency injection extension. I have registered the IRepo type, but I'm not sure what the registration of the Lazy class would look like or if it is even…
67
votes
3 answers

Property initializers run before 'self' is available

Seems like I'm having a problem with something that shouldn't be the case... But I would like to ask for some help. There are some explanations here on the Stack I don't get. Having two simple classes where one refers to another, as per below: class…
RafalK
  • 749
  • 2
  • 7
  • 13
57
votes
14 answers

Lazy field initialization with lambdas

I would like to implement lazy field initialization (or deferred initialization) without an if statement and taking advantage of lambdas. So, I would like to have the same behavior of the following Foo property but without the if: class A{ …
rodolfino
  • 639
  • 1
  • 5
  • 8
57
votes
4 answers

Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans

I have been suffering from infamous hibernate exception org.hibernate.LazyInitializationException: could not initialize proxy - no Session Now the community is cheering over saying…
Sachin Verma
  • 3,712
  • 10
  • 41
  • 74
55
votes
4 answers

Lazy Var vs Let

I want to use Lazy initialization for some of my properties in Swift. My current code looks like this: lazy var fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() The thing is that once the…
YogevSitton
  • 10,068
  • 11
  • 62
  • 95
53
votes
9 answers

How to solve the LazyInitializationException when using JPA and Hibernate

I am working on a project for a customer who wants to use lazy initialization. They always get "lazy initialization exception" when mapping classes with the default lazy loading mode. @JoinTable(name = "join_profilo_funzionalita", joinColumns =…
rupertin
42
votes
7 answers

How Hibernate.initialize() works

I know to use lazily load objects/collections outside the session, we do Hibernate.initialize(Object obj) so that object that is passed as an argument to initialize() method is initialized and can be used outside of the scope of the session. But…
Anand
  • 20,708
  • 48
  • 131
  • 198
40
votes
5 answers

Singleton lazy vs eager instantiation

If a singleton is implemented as follows, class Singleton { private static Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } } How is this implementation different from the lazy…
java_geek
  • 17,585
  • 30
  • 91
  • 113
40
votes
5 answers

Kotlin lazy properties and values reset: a resettable lazy delegate

So I use kotlin for android, and when inflating views, I tend to do the following: private val recyclerView by lazy { find(R.id.recyclerView) } This method will work. However, there is a case in which it will bug the app. If this is a…
johnny_crq
  • 4,291
  • 5
  • 39
  • 64
38
votes
9 answers

Thread safe lazy construction of a singleton in C++

Is there a way to implement a singleton object in C++ that is: Lazily constructed in a thread safe manner (two threads might simultaneously be the first user of the singleton - it should still only be constructed once). Doesn't rely on static…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
35
votes
3 answers

Lazy initialisation and retain cycle

While using lazy initialisers, is there a chance of having retain cycles? In a blog post and many other places [unowned self] is seen class Person { var name: String lazy var personalizedGreeting: String = { [unowned self] in …
35
votes
4 answers

Are lazy vars in Swift computed more than once?

Are lazy vars in Swift computed more than once? I was under the impression that they replaced the: if (instanceVariable) { return instanceVariable; } // set up variable that has not been initialized Paradigm from Objective-C (lazy…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
32
votes
3 answers

why is Lazy constrained to static contexts?

I'd like to use Lazy T to implement memoization but the initialization function appears to require a static context. For example, the following code refuses to compile, warning that non-static members a and b are inaccessible. It's not clear to me…
Daniel Schobel
  • 498
  • 4
  • 12
31
votes
5 answers

Python class member lazy initialization

I would like to know what is the python way of initializing a class member but only when accessing it, if accessed. I tried the code below and it is working but is there something simpler than that? class MyClass(object): _MY_DATA = None …
user1919510
1
2 3
46 47