<h2class="title"style="clear: both"><aid="program_errorret"></a>Error returns to applications</h2>
</div>
</div>
</div>
<p>
Except for the historic <ahref="../api_reference/C/dbm.html"class="olink">dbm</a>, <ahref="../api_reference/C/dbm.html"class="olink">ndbm</a> and <ahref="../api_reference/C/hsearch.html"class="olink">hsearch</a> interfaces, Berkeley DB
does not use the global variable <codeclass="literal">errno</code> to return
error values. The return values for all Berkeley DB functions are
grouped into the following three categories:
</p>
<divclass="variablelist">
<dl>
<dt>
<spanclass="term">0</span>
</dt>
<dd>A return value of 0 indicates that the operation was successful.</dd>
<dt>
<spanclass="term">> 0</span>
</dt>
<dd>A return value that is greater than 0 indicates that there was a system
error. The <spanclass="bold"><strong>errno</strong></span> value returned by the system is returned by
the function; for example, when a Berkeley DB function is unable to allocate
memory, the return value from the function will be ENOMEM.</dd>
<dt>
<spanclass="term">< 0</span>
</dt>
<dd>A return value that is less than 0 indicates a condition that was not
a system failure, but was not an unqualified success, either. For
example, a routine to retrieve a key/data pair from the database may
return DB_NOTFOUND when the key/data pair does not appear in
the database; as opposed to the value of 0, which would be returned if
the key/data pair were found in the database.
<p>
All values returned by Berkeley DB functions are less than 0 in order to avoid
conflict with possible values of <spanclass="bold"><strong>errno</strong></span>. Specifically, Berkeley DB
reserves all values from -30,800 to -30,999 to itself as possible error
values. There are a few Berkeley DB interfaces where it is possible for an
application function to be called by a Berkeley DB function and subsequently
fail with an application-specific return. Such failure returns will be
passed back to the function that originally called a Berkeley DB interface.
To avoid ambiguity about the cause of the error, error values separate
from the Berkeley DB error name space should be used.</p></dd>
</dl>
</div>
<p>
Although possible error returns are specified by each individual
function's manual page, there are a few error returns that deserve
general mention:
</p>
<p>
<spanclass="bold"><strong>DB_NOTFOUND and DB_KEYEMPTY</strong></span>
</p>
<p>There are two special return values that are similar in meaning and that
are returned in similar situations, and therefore might be confused:
DB_NOTFOUND and DB_KEYEMPTY.</p>
<p><aid="program_errorret.DB_NOTFOUND"></a>The DB_NOTFOUND error return indicates that the requested key/data
pair did not exist in the database or that start-of- or end-of-file has
been reached by a cursor.</p>
<p><aid="program_errorret.DB_KEYEMPTY"></a>The DB_KEYEMPTY error return indicates that the requested
key/data pair logically exists but was never explicitly created by the
application (the Recno and Queue access methods will automatically
create key/data pairs under some circumstances; see <ahref="../api_reference/C/dbopen.html"class="olink">DB->open()</a>
for more information), or that the requested key/data pair was deleted
and never re-created. In addition, the Queue access method will return
DB_KEYEMPTY for records that were created as part of a
transaction that was later aborted and never re-created.</p>
The DB_KEYEXIST error return indicates the <ahref="../api_reference/C/dbput.html#put_DB_NOOVERWRITE"class="olink">DB_NOOVERWRITE</a> option was specified
when inserting a key/data pair into the database and the key already
exists in the database, or the <ahref="../api_reference/C/dbput.html#put_DB_NODUPDATA"class="olink">DB_NODUPDATA</a> option was specified and the
When multiple threads of control are modifying the database, there is
normally the potential for deadlock. In Berkeley DB, deadlock is
signified by an error return from the Berkeley DB function of the value
DB_LOCK_DEADLOCK. Whenever a Berkeley DB function returns
DB_LOCK_DEADLOCK, the enclosing transaction should be aborted.
</p>
<p>
Any Berkeley DB function that attempts to acquire locks can potentially
return DB_LOCK_DEADLOCK. Practically speaking, the safest way to deal
with applications that can deadlock is to anticipate a DB_LOCK_DEADLOCK
return from any <ahref="../api_reference/C/db.html"class="olink">DB</a> or <ahref="../api_reference/C/dbc.html"class="olink">DBC</a> handle method call, or any <ahref="../api_reference/C/env.html"class="olink">DB_ENV</a>
handle method call that references a database, including the database's
If a lock is requested from the <ahref="../api_reference/C/lockget.html"class="olink">DB_ENV->lock_get()</a> or <ahref="../api_reference/C/lockvec.html"class="olink">DB_ENV->lock_vec()</a> methods with the
<ahref="../api_reference/C/lockvec.html#vec_DB_LOCK_NOWAIT"class="olink">DB_LOCK_NOWAIT</a> flag specified, the method will return DB_LOCK_NOTGRANTED if the lock
is not immediately available.
</p>
<p>
If the <ahref="../api_reference/C/envset_flags.html#envset_flags_DB_TIME_NOTGRANTED"class="olink">DB_TIME_NOTGRANTED</a> flag is specified to the <ahref="../api_reference/C/envset_flags.html"class="olink">DB_ENV->set_flags()</a> method,
database calls timing out based on lock or transaction timeout values
will return DB_LOCK_NOTGRANTED instead of DB_LOCK_DEADLOCK.