Db::cursor()
Prev | Chapter 3. The Dbc Handle | Next

## Db::cursor()

    #include <db_cxx.h>

    int
    Db::cursor(DbTxn *txnid, Dbc **cursorp, u_int32_t flags);

The `Db::cursor()` method returns a created database cursor.

Cursors may span threads, but only serially, that is, the application must serialize access to the cursor handle.

The `Db::cursor()` method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success.

### Parameters

#### cursorp

The cursorp parameter references memory into which a pointer to the allocated cursor is copied.

#### flags

The flags parameter must be set to 0 or by bitwise inclusively OR'ing together one or more of the following values:

- `DB_BULK`

  Configure a cursor to optimize for bulk operations. Each successive operation on a cursor configured with this flag attempts to continue on the same database page as the previous operation, falling back to a search if a different page is required. This avoids searching if there is a high degree of locality between cursor operations. This flag is currently only effective with the btree access method: for other access methods it is ignored.

- `DB_READ_COMMITTED`

  Configure a transactional cursor to have degree 2 isolation. This ensures the stability of the current data item read by this cursor but permits data read by this cursor to be modified or deleted prior to the commit of the transaction for this cursor.

- `DB_READ_UNCOMMITTED`

  Configure a transactional cursor to have degree 1 isolation. Read operations performed by the cursor may return modified but not yet committed data. Silently ignored if the DB_READ_UNCOMMITTED flag was not specified when the underlying database was opened.

- `DB_WRITECURSOR`

  Specify that the cursor will be used to update the database. The underlying database environment must have been opened using the DB_INIT_CDB flag.

- `DB_TXN_SNAPSHOT`

  Configure a transactional cursor to operate with read-only snapshot isolation. For databases with the DB_MULTIVERSION flag set, data values will be read as they are when the cursor is opened, without taking read locks.

  This flag implicitly begins a transaction that is committed when the cursor is closed.

  This flag is silently ignored if DB_MULTIVERSION is not set on the underlying database or if a transaction is supplied in the txnid parameter.

#### txnid

To transaction-protect cursor operations, cursors must be opened and closed within the context of a transaction. The txnid parameter specifies the transaction context in which the cursor may be used.

Cursor operations are not automatically transaction-protected, even if the DB_AUTO_COMMIT flag is specified to the DbEnv::set_flags() or Db::open() methods. If cursor operations are to be transaction-protected, the txnid parameter must be a transaction handle returned from DbEnv::txn_begin(); otherwise, NULL.

### Errors

The `Db::cursor()` method may fail and throw a DbException exception, encapsulating one of the following non-zero errors, or return one of the following non-zero errors:

#### DbRepHandleDeadException or DB_REP_HANDLE_DEAD

When a client synchronizes with the master, it is possible for committed transactions to be rolled back. This invalidates all the database and cursor handles opened in the replication environment. Once this occurs, an attempt to use such a handle will throw a DbRepHandleDeadException (if your application is configured to throw exceptions), or return `DB_REP_HANDLE_DEAD`. The application will need to discard the handle and open a new one in order to continue processing.

#### DbDeadlockException or DB_REP_LOCKOUT

The operation was blocked by client/master synchronization.

DbDeadlockException is thrown if your Berkeley DB API is configured to throw exceptions. Otherwise, `DB_REP_LOCKOUT` is returned.

#### EINVAL

An invalid flag value or parameter was specified.

### Class

Db

### See Also

Database Cursors and Related Methods

Prev | Up | Next
Chapter 3. The Dbc Handle | Home | Dbc::close()