I have some code I'm maintaining that makes an embedded JDBC call within another. This smells wrong to me but more importantly I'm wondering if this will cause a connection leak?
public Table1 method1(){
String query = "SELECT id1, id2 FROM table1";
Table1 t1 = new Table();
try (Connection masterDB = getConnection();
PreparedStatement statement = masterDB.prepareStatement(query);
ResultSet result = statement.executeQuery()) {
while (result.next()) {
t1.setEntry(method2(result.getInt("id1")));
}
}
return t1;
}
public Table2 method2(){
String query = "SELECT idA, idB FROM table2";
Table2 t2 = new Table2();
try (Connection masterDB = getConnection();
PreparedStatement statement = masterDB.prepareStatement(query);
ResultSet result = statement.executeQuery()) {
while (result.next()) {
t2.setId((result.getInt("idA")));
}
}
return t2;
}