08.01.2015 Views

phsjhxx

phsjhxx

phsjhxx

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

30. Always close locally opened cursors. (CodeXpert)<br />

[CodeXpert 2601]<br />

Reason: Any cursors left open can consume additional System Global Area (i.e.<br />

SGA) memory space within the database instance, potentially in both the<br />

shared and private SQL pools. Furthermore, failure to explicitly close<br />

cursors may also cause the owning session to exceed its maximum limit of<br />

open cursors (as specified by the OPEN_CURSORS database initialization<br />

parameter), potentially resulting in the Oracle error of “ORA-01000:<br />

maximum open cursors exceeded”. For example, the following procedure<br />

opens and fetches, but does not close its cursor – which may cause<br />

problems like those described above.<br />

Example:<br />

-- bad<br />

CREATE PROCEDURE not_close_cursor (out_count OUT INTEGER)<br />

AS<br />

CURSOR c1<br />

IS<br />

BEGIN<br />

SELECT COUNT (*)<br />

FROM all_users;<br />

out_count := 0;<br />

OPEN c1;<br />

FETCH c1<br />

INTO out_count;<br />

END not_close_cursor;<br />

...<br />

-- Good<br />

CREATE PROCEDURE close_cursor (out_count OUT INTEGER)<br />

AS<br />

CURSOR c1<br />

IS<br />

SELECT COUNT (*)<br />

FROM all_users;<br />

BEGIN<br />

out_count := 0;<br />

OPEN c1;<br />

FETCH c1<br />

INTO out_count;<br />

CLOSE c1<br />

END close_cursor;<br />

PL/SQL Coding Guidelines 27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!