0

I would like to see if it is possible to optimize the following code. Indeed before I was in php and the execution was much faster. but I had to switch to java and unfortunately the execution of the code takes twice as long. Does anyone have a solution to give me to improve this code.

This code is used to exploit a resultset to feed a database.

private String queryExtractor(JdbcTemplate template, Integer idExecution, String replaceSql, RequeteLite req ){
        return (String) template.query(replaceSql, new ResultSetExtractor() {           
            int idRequete = req.getIdRequete();

            @Override
            public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                //parcourir nb de ligne
                while(rs.next()){
                    String cleAno="";
                    cleAno= String.format("%sid_requete=%s", cleAno,req.getIdRequete());
                    Map<String,String> hmap = new HashMap<>();
                    //parcourir nb de colonne
                    for (int j = 1; j <= columnCount ; j++) {
                        String colName = rsmd.getColumnName(j);
                        Integer coltype = rsmd.getColumnType(j);
                        boolean test = coltype.equals(java.sql.Types.DOUBLE);
                        if(test){
                            Double colValeur = rs.getDouble(j);
                            String valeur = colValeur.toString();
                            if("".equals(valeur) || valeur == null){
                                valeur = "NULL";
                            }
                            hmap.put(colName, valeur);
                        
                            
                        }else{
                            String colValeur = rs.getString(j);
                            if("".equals(colValeur) || colValeur == null){
                                colValeur = "NULL";
                            }
                            hmap.put(colName, colValeur);
                        }
                                             
                    }
                    Map<String,String> sortedMap = new TreeMap<>(hmap);

                    for(Map.Entry<String, String> entry : sortedMap.entrySet()) {
                        cleAno = String.format("%s&%s=%s", cleAno,entry.getKey(),entry.getValue()); 
                    }

                    //On vérifie si l'anomalie existe déja.                                   
                    if(!ligneAnomalieDAO.existAnomalie(cleAno)){ 
                        //L'anomalie n'existe pas
                        //On insère l'anomalie
                        LigneAnomalie ligneAno = new LigneAnomalie(idRequete, cleAno);
                        Integer idLigneAnomalie = ligneAnomalieManager.addLigne(ligneAno);

                        //On insère les champs dans la table champLigneAnomalie             
                        for(Map.Entry<String, String> entry : sortedMap.entrySet()) {
                            ChampLigneAnomalie champLigneAno = new ChampLigneAnomalie(idLigneAnomalie, entry.getKey(), entry.getValue());
                            champligneAnoManager.addChamp(champLigneAno);             
                        }                                       
                    }else{
                        //l'anomalie existe
                        //On vérifie si l'anomalie possède une justification
                        Integer idLigneAno = ligneAnomalieDAO.findLigneByCle(cleAno).getIdLigneAnomalie();
                        if(justificationDAO.existJustification(idLigneAno)){
                            //L'anomalie possède une justification
                            JustificationAnomalie justificationAno  = justificationDAO.findJustifByIdLigneAnomalie(idLigneAno);
                            if(justificationAno.getType().equals("correction")){
                                // la justification est une correction, on annule la correction car il ne devrait pas avoir d'erreur si la ligne est corrigée
                                justificationAno.setCommentaire("L annomalie n a pas été corrigée (La requête "+ req.getTitre() +" a de nouveau rencontré l anomalie)");
                                justificationAno.setValidJustification(0);
                                justificationDAO.updateJustif(justificationAno);
                            }  
                        }
                    }
                    Integer idLigneAno = ligneAnomalieDAO.findLigneByCle(cleAno).getIdLigneAnomalie();
                    LLigneAnomalieExecutionJeu lligneAnoExe = new LLigneAnomalieExecutionJeu(idExecution, idLigneAno);
                    lligneAnoExeManager.addLLAE(lligneAnoExe);
                }
                return null;
            }           
        });        
    } 
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
sneck
  • 1
  • 1
  • In general, you are creating a lot of objects inside loops, and this will hit performance. And this is happening for every record you retrieve. Additionally, your method of testing a string for empty, automatically incudes the null check, so you do not need to do the null check as well. There are probably other places where improvements could be made, but this is a start. – jr593 Jul 18 '22 at 06:45
  • Have you analyzed where your code spends most time? Try to run a profiler: https://stackoverflow.com/questions/22250303/java-application-profiling – Queeg Jul 18 '22 at 06:46
  • I’m voting to close this question because it belongs on https://codereview.stackexchange.com/ – MyStackRunnethOver Jul 18 '22 at 17:33

0 Answers0