Skip to content

Commit

Permalink
#470: fixe resource leak and copy-constructor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerdes authored and rtroilo committed Nov 11, 2022
1 parent c55c0cc commit 61d58ed
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
Expand Down Expand Up @@ -38,8 +39,9 @@ abstract class MapReducerJdbc<X> extends MapReducer<X> implements CancelableProc
}

// copy constructor
MapReducerJdbc(MapReducerJdbc obj) {
super(obj);
MapReducerJdbc(MapReducerJdbc<?> source) {
super(source);
this.executionStartTimeMillis = source.executionStartTimeMillis;
}

@Override
Expand Down Expand Up @@ -83,7 +85,7 @@ protected Stream<GridOSHEntity> getOshCellsStream(CellIdRange cellIdRange) {
pstmt.setLong(3, cellIdRange.getEnd().getId());
var oshCellsRawData = pstmt.executeQuery();
return StreamSupport.stream(spliteratorUnknownSize(
new GridOSHEntityIterator(oshCellsRawData, conn), 0
new GridOSHEntityIterator(oshCellsRawData, pstmt, conn), 0
), false);
} catch (SQLException e) {
throw new OSHDBException(e);
Expand All @@ -93,11 +95,13 @@ protected Stream<GridOSHEntity> getOshCellsStream(CellIdRange cellIdRange) {
private class GridOSHEntityIterator implements Iterator<GridOSHEntity> {

private final ResultSet oshCellsRawData;
private final PreparedStatement preparedStatement;
private final Connection conn;
GridOSHEntity next;

public GridOSHEntityIterator(ResultSet oshCellsRawData, Connection conn) {
public GridOSHEntityIterator(ResultSet oshCellsRawData, PreparedStatement pstmt, Connection conn) {
this.oshCellsRawData = oshCellsRawData;
this.preparedStatement = pstmt;
this.conn = conn;
}

Expand All @@ -121,6 +125,7 @@ private GridOSHEntity getNext() {
if (!oshCellsRawData.next()) {
try {
oshCellsRawData.close();
preparedStatement.close();
} finally {
conn.close();
}
Expand Down

0 comments on commit 61d58ed

Please sign in to comment.