Take the object from the list or make a new select in the database?

Asked

Viewed 12 times

-1

I have a Controller that the first thing he does is bring a list of objects from the bank. After clicking on one of the buttons, the customer will use one of the items in the list.

My question is the following: What would be the best way to get this object? Gives list I already have, or I make a new select in the bank?

@PostConstruct
    public void init() {
        int IdPaciente = Integer.parseInt(Str.NullToStrDef(FaceUtil.getRequest().getParameter("idpaciente"), "0"));
        paciente = pacienteDao.find(IdPaciente);
        listProgramas = programaDao.findByAplicador(loginController.getUsuarioLogado().getIdusuario(), IdPaciente);
        
        int IdPrograma = Integer.parseInt(Str.NullToStrDef(FaceUtil.getRequest().getParameter("programa"), "0"));
        if(IdPrograma > 0) {

            programaAplicando = programaDao.find(IdPrograma); //Assim?
            
            programaAplicando = listProgramas.stream()
                    .filter(t -> t.getIdprograma() == IdPrograma)
                    .collect(Collectors.toList()).get(0); //ou assim?
        }
}

1 answer

0

@Alexandre Biancuzzi, It is a little difficult to suggest something, because I do not have all the knowledge of your code and its implementations nor your real need.

Below is my contributions, maybe it will help something:

1 Validations before consuming the database

The cost of the database is very high ( Integrity of the database, data value, competitive value of connections, value in R$ kkkk, application scaling comparison x bank scaling and etc ).

As much as it is possible to make business validations, requests parameter validations, variable preparations before consuming the bank will be better.

2 Bring up-to-date data to the user

I don’t understand what your controller does. More inevitable is the act of always trying to bring the most up-to-date data to the user.

In other words, it will always be necessary to go to the bank to bring the most up-to-date information ( or to use other techniques such as caches and proxies ), but I believe it is not a good practice to search for a list at some point without going to the bank and update it according to what is in the bank. Theoretically in most cases the user will want this information updated in each request ( depending on the business rule of course ).

3 Optimize your bank searches.

The most that is possible to do the smallest number of searches in the bank and the maximum of optimized searches will be for the information traffic, optimization in the bank processing and Diminuicão of the competition of connections to the bank.

Regarding your code, it would be better if you had something in your DAO that did not need to bring the entire program list to the application and will only use the first ex item:

    programaAplicando = programaDao.findOnlyOneObjectByIdPacienteAndIdPrograma(
                             Integer.parseInt(Str.NullToStrDef(FaceUtil.getRequest().getParameter("idpaciente"), "0"),
                             Str.NullToStrDef(FaceUtil.getRequest().getParameter("programa"), "0")
                        );
  • Cool Dilermando! It was +- what I had in mind, but I needed to know if it made "sense". One of the things I always worry about is not making requisitions to the bank unnecessarily. Regarding your suggestion, I really need to bring all the programs, since the user will "take" one of them to run. And since I use a Viewscoped, I don’t need to give another select. In this case, the program is not changed frequently, so I do not need to worry about data competition. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.