views:

50

answers:

1

What's the proper pattern to close all open cursors in a lua script before closing the db connection? I have a helper function rows() that is called in multiple places which creates cursors, on the function end() I want to be able to close all that have been created.

function rows (sql_statement)
   local cursor = assert (con:execute (sql_statement));
   local closed = false;
   return function ()
     if (closed) then return nil end;
     local row = {};
     result = cursor:fetch(row);
     if (result == nil) then
       cursor:close();
       closed = true;
       return nil;
     end;
     return row;
   end
end

function end() 
   -- this con:close() call fails because of open cursors
   con:close();
   env:close();
end
A: 

The iterator function returned by rows() does not close the cursor until the end of the result set is reached. Perhaps some of the instantiations of the iterator did not completely read their results. You can try calling collectgarbage('collect') to clean up any unreferenced iterator functions before closing the connection. The rows() function could also place all cursors in a table with weak references and the end() function could enumerate these closing any open cursors.

gwell