Project

Profile

Help

sql:query leaves open cursors in Oracle

Added by Anonymous over 19 years ago

Legacy ID: #3036823 Legacy Poster: Kevin Rodgers (notorious_kev)

Using a single sql:connection to process a large number of queries using Oracle's JDBC driver (more than the server's OPEN_CURSORS limit) results in this error: ORA-01000: maximum open cursors exceeded That error can be prevented by ensuring that both the statement and result set are closed. Here's the patch: *** net/sf/saxon/sql/SQLQuery.java~Fri Jan 21 08:29:46 2005 --- net/sf/saxon/sql/SQLQuery.javaTue Mar 8 09:27:24 2005 *************** *** 186,191 **** --- 186,193 ---- int rowCode = pool.allocate("", "", rowTag); int colCode = pool.allocate("", "", colTag); + PreparedStatement ps = null; + ResultSet rs = null; try { StringBuffer statement = new StringBuffer(); statement.append("SELECT " + dbCol + " FROM " + dbTab); *************** *** 195,205 **** //System.err.println("-> SQL: " + statement.toString()); // -- Prepare the SQL statement ! PreparedStatement ps=connection.prepareStatement(statement.toString()); controller.setUserData(this, "sql:statement", ps); // -- Execute Statement ! ResultSet rs = ps.executeQuery(); // -- Print out Result Receiver out = context.getReceiver(); --- 197,207 ---- //System.err.println("-> SQL: " + statement.toString()); // -- Prepare the SQL statement ! ps = connection.prepareStatement(statement.toString()); controller.setUserData(this, "sql:statement", ps); // -- Execute Statement ! rs = ps.executeQuery(); // -- Print out Result Receiver out = context.getReceiver(); *************** *** 221,229 **** //System.out.println(rowEnd); out.endElement(); } ! rs.close(); ! ! if (!connection.getAutoCommit()) { connection.commit(); } --- 223,229 ---- //System.out.println(rowEnd); out.endElement(); } ! if (!connection.getAutoCommit()) { connection.commit(); } *************** *** 231,236 **** --- 231,248 ---- DynamicError de = new DynamicError("(SQL) " + ex.getMessage()); de.setXPathContext(context); throw de; + } finally { + try { + if (rs != null) + rs.close(); + if (ps != null) + ps.close(); + } catch (SQLException ex) { + DynamicError de = + new DynamicError("(SQL) " + ex.getMessage()); + de.setXPathContext(context); + throw de; + } } } }


Replies (2)

RE: sql:query leaves open cursors in Oracle - Added by Anonymous over 19 years ago

Legacy ID: #3036854 Legacy Poster: Brett Knights (bknights)

If you really want to make sure you end up with all your oracle resources released you'd be better off with something like: DynamicError de = null; // declare de so it's visible to all appropriate scopes. ... finally{ boolean wasDEThrown = (de != null); if (rs != null){ try{ rs.close(); } catch (SQLException ex) { de = new DynamicError("(SQL) " + ex.getMessage()); e.setXPathContext(context); } } if (ps != null){ try{ ps.close(); } catch (SQLException ex) { de = new DynamicError("(SQL) " + ex.getMessage()); e.setXPathContext(context); } } if(!wasDEThrown && de != null) throw de; // test so we don't lose the real exception }

RE: sql:query leaves open cursors in Oracle - Added by Anonymous over 19 years ago

Legacy ID: #3037427 Legacy Poster: Michael Kay (mhkay)

I've applied this, using Brett's version of the "finally" block. Thanks for the contribution. Michael Kay

    (1-2/2)

    Please register to reply