Question

I am using JDBC on a PostgreSQL database. When I query for an entity in a resultset, it returns 5 rows. Related to that entity is another entity, for which I query while i am using a row in the above resultset. When I execute this query, the above resultset is closed.

This means that it is allowing only 1 resultset to be active on 1 connection at a time.

Previously the same code was working perfect for Oracle DB server.

Is it that I need to ask the DB admin to configure the server to allow multiple resultsets? Or to do some change in the code? Or is it impossible to do it in postgre? Here is the code for more details:

    Connection conn = PTSConnection.getConnection();

    Statement stmt = conn.createStatement();

    ResultSet lines = stmt.executeQuery("SELECT LINEID,STARTSTOPID,ENDSTOPID FROM LINES"); **//first resultset is active**

    while (lines.next()){
        int lineId= lines.getInt(1);
        Stop ss = StopStorage.getByID(lines.getInt(2));
        Stop es = StopStorage.getByID(lines.getInt(3));
        ResultSet stops = stmt.executeQuery("SELECT STOPID FROM STOPSINLINES WHERE LINEID=" + lineId); **//first resultset dies**

        List<Stop> lineStops = new ArrayList<Stop>();
        while(stops.next()){
            Stop stop = StopStorage.getByID(stops.getInt(1));
            lineStops.add(stop);
        }
        stops.close();

        Line aLine = null;

        ResultSet emergencyLine = stmt.executeQuery("SELECT CAUSE, STARTTIME, ENDTIME FROM EMERGENCYLINES WHERE LINEID =" + lineId);
        if(emergencyLine.next()){
            String cause = emergencyLine.getString(1);
            Time startTime = emergencyLine.getTime(2);
            Time endTime = emergencyLine.getTime(3);

            aLine = new EmergencyLine(ss, es, cause, startTime, endTime, (Stop[]) lineStops.toArray(new Stop[lineStops.size()]));
        } else {
            aLine = new Line(ss, es, (Stop[]) lineStops.toArray(new Stop[lineStops.size()]));
        }

        emergencyLine.close();

        LineRepository.getInstance().addLine(aLine);
    }

    lines.close();
Was it helpful?

Solution

The reason is not that you are using two resultsets on the same connection, but you are re-using the same Statement object for a new query. When you run executeQuery() on a Statement instance, any previous result will be closed (I'm surprised that your code did work with Oracle...)

Simply create a new Statement object before executing the second query:

Statement stmt = conn.createStatement();
Statement nestedStmt = conn.createStatement();

ResultSet lines = stmt.executeQuery("SELECT LINEID,STARTSTOPID,ENDSTOPID FROM LINES"); **//first resultset is active**

while (lines.next()){
  ... 
    ResultSet stops = nestedStmt.executeQuery("SELECT STOPID FROM STOPSINLINES WHERE LINEID=" + lineId); **//first resultset dies**
    List lineStops = new ArrayList();
    while(stops.next()){
        Stop stop = StopStorage.getByID(stops.getInt(1));
        lineStops.add(stop);
    }
    stops.close();

    ...
    ResultSet emergencyLine = nestedStmt.executeQuery("SELECT CAUSE, STARTTIME, ENDTIME FROM EMERGENCYLINES WHERE LINEID =" + lineId);
    if(emergencyLine.next()){
        String cause = emergencyLine.getString(1);

    ....
    }
    emergencyLine.close();

And don't for get to properly close all Statements and ResultSets !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top