Skip to content

Commit 647ff82

Browse files
author
Daniel Junglas
committed
Perform queries in an exception safe way.
This fixes issue #5 for data reading (writing is still buggy).
1 parent a4949a7 commit 647ff82

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

lib/jdbc-custom-data-source.jar

-211 Bytes
Binary file not shown.

src/main/java/com/ibm/opl/customdatasource/JdbcCustomDataSource.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,11 @@ else if (type == Type.STRING) {
244244
}
245245
}
246246

247-
public void readSet(Type leaf, String name, String query) {
247+
public void readSet(Type leaf, String name, String query) throws SQLException {
248248
IloOplDataHandler handler = getDataHandler();
249+
final RunQuery q = new RunQuery(query);
249250
try {
250-
Connection conn = DriverManager.getConnection(_configuration.getUrl(),
251-
_configuration.getUser(),
252-
_configuration.getPassword());
253-
Statement stmt = conn.createStatement();
254-
ResultSet rs = stmt.executeQuery(query);
255-
251+
ResultSet rs = q.getResult();
256252
handler.startElement(name);
257253
handler.startSet();
258254

@@ -266,32 +262,27 @@ else if (leaf == Type.STRING)
266262
}
267263
handler.endSet();
268264
handler.endElement();
269-
rs.close();
270-
stmt.close();
271-
conn.close();
272-
} catch (SQLException e) {
273-
e.printStackTrace();
265+
}
266+
finally {
267+
q.close();
274268
}
275269
}
276270

277-
public void readTupleSet(String name, String query) {
271+
public void readTupleSet(String name, String query) throws SQLException {
278272
IloOplDataHandler handler = getDataHandler();
279-
try {
280-
IloOplElement elt = handler.getElement(name);
281-
ilog.opl_core.cppimpl.IloTupleSet tupleSet = (ilog.opl_core.cppimpl.IloTupleSet) elt.asTupleSet();
282-
IloTupleSchema schema = tupleSet.getSchema_cpp();
283-
int size = schema.getTotalColumnNumber();
273+
IloOplElement elt = handler.getElement(name);
274+
ilog.opl_core.cppimpl.IloTupleSet tupleSet = (ilog.opl_core.cppimpl.IloTupleSet) elt.asTupleSet();
275+
IloTupleSchema schema = tupleSet.getSchema_cpp();
276+
int size = schema.getTotalColumnNumber();
284277

285-
String[] oplFieldsName = new String[size];
286-
Type[] oplFieldsType = new Type[size];
278+
String[] oplFieldsName = new String[size];
279+
Type[] oplFieldsType = new Type[size];
287280

288-
fillNamesAndTypes(schema, oplFieldsName, oplFieldsType);
281+
fillNamesAndTypes(schema, oplFieldsName, oplFieldsType);
289282

290-
Connection conn = DriverManager.getConnection(_configuration.getUrl(),
291-
_configuration.getUser(),
292-
_configuration.getPassword());
293-
Statement stmt = conn.createStatement();
294-
ResultSet rs = stmt.executeQuery(query);
283+
final RunQuery q = new RunQuery(query);
284+
try {
285+
ResultSet rs = q.getResult();
295286

296287
handler.startElement(name);
297288
handler.startSet();
@@ -311,12 +302,9 @@ public void readTupleSet(String name, String query) {
311302
}
312303
handler.endSet();
313304
handler.endElement();
314-
rs.close();
315-
stmt.close();
316-
conn.close();
317-
} catch (SQLException e) {
318-
e.printStackTrace();
319305
}
320-
306+
finally {
307+
q.close();
308+
}
321309
}
322310
};

0 commit comments

Comments
 (0)