DbEnv::lock_vec()
Prev | Chapter 7. The DbLock Handle | Next

## DbEnv::lock_vec()

    #include <db_cxx.h>

    int
    DbEnv::lock_vec(u_int32_t locker, u_int32_t flags,
        DB_LOCKREQ list[], int nlist, DB_LOCKREQ **elistp);

The `DbEnv::lock_vec()` method atomically obtains and releases one or more locks from the lock table. The `DbEnv::lock_vec()` method is intended to support acquisition or trading of multiple locks under one lock table semaphore, as is needed for lock coupling or in multigranularity locking for lock escalation.

If any of the requested locks cannot be acquired, or any of the locks to be released cannot be released, the operations before the failing operation are guaranteed to have completed successfully, and `DbEnv::lock_vec()` returns a non-zero value. In addition, if elistp is not NULL, it is set to point to the DB_LOCKREQ entry that was being processed when the error occurred.

Unless otherwise specified, the `DbEnv::lock_vec()` 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

#### elistp

If an error occurs, and the elistp parameter is non-NULL, it is set to point to the DB_LOCKREQ entry that was being processed when the error occurred.

#### flags

The flags parameter must be set to 0 or the following value:

- `DB_LOCK_NOWAIT`

  If a lock cannot be granted because the requested lock conflicts with an existing lock, return DB_LOCK_NOTGRANTED immediately instead of waiting for the lock to become available. In this case, if non-NULL, elistp identifies the request that was not granted.

#### locker

The locker parameter is an unsigned 32-bit integer quantity. It represents the entity requesting or releasing the lock.

#### list

The list array provided to `DbEnv::lock_vec()` is typedef'd as DB_LOCKREQ.

To ensure compatibility with future releases of Berkeley DB, all fields of the DB_LOCKREQ structure that are not explicitly set should be initialized to 0 before the first time the structure is used. Do this by declaring the structure external or static, or by calling memset(3).

A DB_LOCKREQ structure has at least the following fields:

- `lockop_t op;`

  The operation to be performed, which must be set to one of the following values:

  - `DB_LOCK_GET`

    Get the lock defined by the values of the mode and obj structure fields, for the specified locker. Upon return from `DbEnv::lock_vec()`, if the lock field is non-NULL, a reference to the acquired lock is stored there. (This reference is invalidated by any call to `DbEnv::lock_vec()` or DbEnv::lock_put() that releases the lock.)

  - `DB_LOCK_GET_TIMEOUT`

    Identical to DB_LOCK_GET except that the value in the timeout structure field overrides any previously specified timeout value for this lock. A value of 0 turns off any previously specified timeout.

  - `DB_LOCK_PUT`

    The lock to which the lock structure field refers is released. The locker parameter, and mode and obj fields are ignored.

  - `DB_LOCK_PUT_ALL`

    All locks held by the specified locker are released. The lock, mode, and obj structure fields are ignored. Locks acquired in operations performed by the current call to `DbEnv::lock_vec()` which appear before the DB_LOCK_PUT_ALL operation are released; those acquired in operations appearing after the DB_LOCK_PUT_ALL operation are not released.

  - `DB_LOCK_PUT_OBJ`

    All locks held on obj are released. The locker parameter and the lock and mode structure fields are ignored. Locks acquired in operations performed by the current call to `DbEnv::lock_vec()` that appear before the DB_LOCK_PUT_OBJ operation are released; those acquired in operations appearing after the DB_LOCK_PUT_OBJ operation are not released.

  - `DB_LOCK_TIMEOUT`

    Cause the specified locker to timeout immediately. If the database environment has not configured automatic deadlock detection, the transaction will timeout the next time deadlock detection is performed. As transactions acquire locks on behalf of a single locker ID, timing out the locker ID associated with a transaction will time out the transaction itself.

- `DB_LOCK lock;`

  A lock reference.

- `const lockmode_t mode;`

  The lock mode, used as an index into the environment's lock conflict matrix. When using the default lock conflict matrix, mode must be set to one of the following values:

  - `DB_LOCK_READ`

    read (shared)

  - `DB_LOCK_WRITE`

    write (exclusive)

  - `DB_LOCK_IWRITE`

    intention to write (shared)

  - `DB_LOCK_IREAD`

    intention to read (shared)

  - `DB_LOCK_IWR`

    intention to read and write (shared)

  See DbEnv::set_lk_conflicts() and Standard Lock Modes for more information on the lock conflict matrix.

- const DBT obj;

  An untyped byte string that specifies the object to be locked or released. Applications using the locking subsystem directly while also doing locking via the Berkeley DB access methods must take care not to inadvertently lock objects that happen to be equal to the unique file IDs used to lock files. See Access method locking conventions in the Berkeley DB Programmer's Reference Guide for more information.

- u_int32_t timeout;

  The lock timeout value.

#### nlist

The nlist parameter specifies the number of elements in the list array.

### Errors

The `DbEnv::lock_vec()` 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:

#### DbDeadlockException or DB_LOCK_DEADLOCK

A transactional database environment operation was selected to resolve a deadlock.

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

#### DbLockNotGrantedException or DB_LOCK_NOTGRANTED

A Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable to grant a lock in the allowed time.

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

#### DbLockNotGrantedException or DB_LOCK_NOTGRANTED

The DB_LOCK_NOWAIT flag or lock timers were configured and the lock could not be granted before the wait-time expired.

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

#### EINVAL

An invalid flag value or parameter was specified.

#### ENOMEM

The maximum number of locks has been reached.

### Class

DbEnv, DbLock

### See Also

Locking Subsystem and Related Methods

Prev | Up | Next
DbEnv::lock_stat_print() | Home | Chapter 8. The DbLsn Handle