#The result set is closed

1 messages · Page 1 of 1 (latest)

mint summit
#

Hello,

I'm a student and I've been stuck on a problem for a few days. “The result set is closed”

I want to test my getFromID method, which allows me to load a groupline and the lines in that groupline (then the blocks in that line, and then the devices in that line).

So in the groupline dao I access the line dao to call the getlistefromgroupe method, which lets me return all the lines in a groupline as a list.

Can you help me find the error? Here are the different codes below:

frail flameBOT
#

<@&987246399047479336> please have a look, thanks.

mint summit
#

private Connection connection;
    private DAOFactory fabrique;
    private SQLLigneDao ligneDao;

    /**
     * Construction du dao, 
     * fourni la fabrique pour avoir la connexion
     * @param fabrique
     */
      public SQLGroupeLigneDao(DAOFactory fabrique) {
            this.fabrique = fabrique;
            this.connection = fabrique.getConnection();
            this.ligneDao = (SQLLigneDao) fabrique.getLigneDAO();
            
        }

    
    /**
     * @author Mehdi
     */
    public Optional<GroupeLigne> getFromID(Integer id) {
        GroupeLigne grp = null;
        try (var q = connection.prepareStatement(SQL_GET_FROMID)) {
            q.setInt(1, id);
            ResultSet rs = q.executeQuery();
            while (rs.next()) {
                grp = new GroupeLigne(id);
                
                //Récupération de toutes les lignes du groupe
                List<Ligne> listeLignes = ligneDao.getListeFromGroupe(id);
                
                //Ajout de toutes lignes du groupe au groupe lui même
                 grp.getLignes().addAll(listeLignes);
                
            }
        } catch (SQLException e) {
            log.error("Problème GetFromID " + e.getMessage());
        }
        return Optional.ofNullable(grp);
    }

#

@Override
    public List<Ligne> getListeFromGroupe(Integer id_grp) {
         
        List<Ligne> listeLignes = new ArrayList<Ligne>();
        try (var q = connection.prepareStatement(SQL_GET_LIGNES_FROM_GROUPE)) {
            
            q.setInt(1, id_grp);
            ResultSet rs = q.executeQuery();
             while (rs.next()) {     
                    Cable cable = cableDao.getFromID(rs.getString("FKCABLE_LIG")).orElseThrow();
                    Disjoncteur fusible = disjoncteurDao.getFromID(rs.getString("FKFUSIBLE_LIG")).orElseThrow();
                    
                    //Instanciation d'une ligne
                    Integer id_lig = rs.getInt("NUM_LIG");                    
                     Ligne ligne = new Ligne(id_lig,rs.getString("CODE_LIG"), rs.getBoolean("INTERNE_LIG"), cable , fusible);
                     
                     //Ajouter tous les blocs d'une ligne
                     List<Bloc> blocs = blocDao.getBlocFromLigne(id_lig);
                     ligne.getBlocs().addAll(blocs);
                    
                     listeLignes.add(ligne);
             }
             
         }catch (SQLException e) {
                 log.error("Problème getListeFromGroupe " + e.getMessage());
         }

        return listeLignes;
    }

#

@Test
    void testGetFromID() {
        
        GroupeLigne expectedGroupe = new GroupeLigne(10);
        
        Cable cable = factory.getCableDAO().getFromID("XVB3G2").orElseThrow();
        Disjoncteur fusible = factory.getDisjoncteurDAO().getFromID("FU22").orElseThrow();
                            
         Ligne ligne1 = new Ligne(1000, null, true, cable , fusible);
         Ligne ligne2 = new Ligne(1001, null, true, cable , fusible);
         
         
         GroupeLigne actualGroupeLigne = groupeLigneDao.getFromID(10).orElseThrow();
                
        // Comparer l'objet attendu avec celui récupéré
        assertEquals(expectedGroupe, expectedGroupe);
    }

#

Error : ```java [main] ERROR dao.SQLLigneDao - Problème getListeFromGroupe The result set is closed

oak hawk
sour jewel
#

It sounds like you're executing another statement in the same connection before you've finished traversing the results from the first statement.

mint summit
mint summit
sour jewel
oak hawk
#

OR

#

you can have your query fetch all the nested things in one go

#

that last one is an art I am still figuring out how to do best

#

but "CROSS JOIN LATERAL" is something i need to look deeper into

mint summit
#

But that's my teacher's class, and he calls another dao while he's still in the result set but that doesn't close it. I don't get it 🥲

mint summit
crimson basalt