I want to add all the data from the database to the List when the server is being raised with the application in order to work with them in the future b requests to the database when the page is updated. When placing a query in the database in the constructor or static{} displays NPE, the List in which I request the data from the database is empty.

As I understand it, the request comes before the connection to the database is established. How do I implement this? And is it worth doing this if we assume that a million data lines can be placed in the database?
Thanks in advance.

  • one
    Pretty incomprehensible without the code that you mean. If you want something like a cache, then it is better to use tools suitable for this, like ehcache . - sanluck
  • You are absolutely right, only I have already found the answer. - Dmitriy Smirnov

1 answer 1

When placing a query in the database in the constructor or static {} displays NPE

This is a logical and expected behavior: a static block is executed if I remember correctly when the JVM loads the class definition (meaning the .class file) into memory. The constructor is executed when creating a bean, but before Spring injects all dependencies into it.

How do I implement this?

There are many options, but I advise you to add a method with code to load data from the database and annotate it with the annotation @PostConstruct

And is it worth doing this if we assume that a million data lines can be placed in the database?

Most likely not, not worth it. The data will take up memory and probably will not be used all the time.