ConnectionHandler.getConnection() conundrum

User 870ab5b546

15-01-2015 18:09:39

We had some code that was working with JChem 14.11.10 but then stopped working in JChem 14.12.15. With the more recent JChem, we got a "Connection closed" exception thrown and caught in setRxnCondition(), after ReactorResultsRW.deleteCalcdProducts() had already run. We figured out that we had a bug in deleteCalcdProducts(), where we were closing the Connection passed to deleteCalcdProducts() with the closeConHandler() method, even though we needed to keep the connection open so that setRxnCondition() could use it again. So that explained why the code didn't work in JChem 14.12.15. What we can't figure out is why the code worked when compiled with JChem 14.11.10! The best I can figure is that in JChem 14.11.10, the method ConnectionHandler.getConnection(), which we call in closeConHandler(), doesn't actually return the original Connection used to construct the ConnectionHandler, so the original Connection is not closed when it returns to setRxnCondition(); but that by JChem 14.12.15, you fixed that bug, and that's why the buggy code stopepd working, as it never should have worked ot begin with. Is my analysis correct?


private static void setRxnCondition(Connection con,
RxnCondition rxnCondn) throws DBException {
final String SELF = "RxnCondnRW.setRxnCondition: ";
try {
con.setAutoCommit(false);
final StringBuilder qryBld = new StringBuilder();
// first, delete products calculated from SMs under the old definition
ReactorResultsRW.deleteCalcdProducts(con, rxnCondn.rxnId);
qryBld.append(UPDATE + RXN_CONDNS + SET).append(updateSet(
RXNCOND_NAME, quotes(rxnCondn.name),
RXNCOND_CLASS, quotes(rxnCondn.classifn),
RXNCOND_3COMP, quotes(rxnCondn.threeComponent ? 'Y' : 'N'),
RXNCOND_DEF, QMARK)) // CLOB field
.append(WHERE + RXNCOND_ID + EQUALS)
.append(rxnCondn.rxnId);
final String qry = qryBld.toString();
debugPrint(SELF, qry);
tryUpdate(con, qry, rxnCondn.reactionDef);
con.commit();
} catch (SQLException e) {
rollbackConnection(con);
throw new DBException(e.getMessage());
}
} // setRxnCondition(Connection, RxnCondition)

static void deleteCalcdProducts(Connection con, int rxnId)
throws DBException {
final String SELF = "ReactorResultsRW.deleteCalcdProducts: ";
final String where = new StringBuilder()
.append(WHERE + REACT_RXN_ID + EQUALS).append(rxnId)
.toString();
ConnectionHandler conHandler = null;
try {
conHandler = getConHandler(con);
UpdateHandler.deleteRows(conHandler, REACTOR_RESULTS, where);
alwaysPrint(SELF + "deleted calculated products of reaction ",
rxnId);
} catch (SQLException e) {
alwaysPrint(SELF + "caught SQLException.");
e.printStackTrace();
throw new DBException(e.getMessage());
} finally {
closeConHandler(conHandler);
} // try
} // deleteCalcdProducts(Connection, int)

private static void closeConHandler(ConnectionHandler conHandler) {
if (conHandler != null) {
closeConnection(conHandler.getConnection());
} // if conHandler is not null
} // closeConHandler(ConnectionHandler)

protected static void closeConnection(Connection con) {
try { if (con != null) con.close(); }
catch (Exception e) { e.printStackTrace(); }
} // closeConnection(Connection)

ChemAxon d9cc14700b

16-01-2015 14:42:37

Hi,


It's quite strange since we have not changed the ConnectionHandler class in the mentioned time interval and it always returned and still returns the original connection, not a copy.


Best Regards,
GaborĀ