Cursor considerations for CLI applications
Which cursor type to use
The first decision to make is between a forward-only cursor and a scrollable cursor. A forward-only cursor incurs less resource usage than a scrollable cursor, and scrollable cursors have the potential for decreased concurrency.
If your application does not need the additional features of a scrollable cursor, then you should use a non-scrollable cursor.
If the application needs to detect changes to the underlying data or needs to add, update, or delete data from the cursor, then the application must use either a keyset-driven or dynamic cursor. To perform updates and deletions on rows in a dynamic scrollable cursor's result set, the UPDATE or DELETE statement must include all the columns of at least one unique key in the base table. This can be the primary key or any other unique key. Because dynamic cursors incur more resource usage and might have less concurrency than keyset-driven cursors, only choose dynamic cursors if the application needs to detect both changes made and rows inserted by other cursors.
If an application requests a scrollable cursor that can detect changes without specifying a particular cursor type, then CLI will assume that a dynamic cursor is not needed and provide a keyset-driven cursor. This behavior avoids the increased resource usage and reduced concurrency that is incurred with dynamic cursors.
SQLGetInfo()
with
an InfoType of: - SQL_DYNAMIC_CURSOR_ATTRIBUTES1
- SQL_DYNAMIC_CURSOR_ATTRIBUTES2
- SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1
- SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2
- SQL_KEYSET_CURSOR_ATTRIBUTES1
- SQL_KEYSET_CURSOR_ATTRIBUTES2
- SQL_STATIC_CURSOR_ATTRIBUTES1
- SQL_STATIC_CURSOR_ATTRIBUTES2
Unit of work considerations
A cursor can
be closed either explicitly or implicitly. An application can explicitly
close a cursor by calling SQLCloseCursor()
.
Any further attempts to manipulate the cursor will result in error,
unless the cursor is opened again. The implicit closure of a cursor
depends on a several factors including how the cursor was declared
and whether or not a COMMIT or ROLLBACK occurs.
- Set the statement attribute SQL_ATTR_CURSOR_HOLD to SQL_CURSOR_HOLD_ON (default) or SQL_CURSOR_HOLD_OFF. This setting only affects cursors opened on the statement handle after this value has been set. It will not affect cursors already open.
- Set the CLI/ODBC configuration keyword CursorHold to change the default CLI driver behavior. Setting CursorHold=1 preserves the default behavior of cursors declared as WITH HOLD, and CursorHold=0 results in cursors being closed when each transaction is committed. You can override this keyword by setting the SQL_ATTR_CURSOR_HOLD statement attribute.
Troubleshooting for applications created before scrollable cursor support
db2cli.ini
file:
Configuration keyword setting | Description |
---|---|
Patch2=6 |
Returns a message that scrollable cursors (keyset-driven, dynamic and static) are not supported. CLI automatically downgrades any request for a scrollable cursor to a forward-only cursor. |
DisableKeysetCursor=1 |
Disables keyset-driven scrollable cursors. This can be used to force the CLI driver to give the application a static cursor when a keyset-driven or dynamic cursor is requested. |