The <codeclass="methodname">DB->close()</code> method flushes cached database information to
disk, closes any open cursors, frees allocated resources, and
closes underlying files. When the close operation for a cursor fails,
the method returns a non-zero error value for the first instance of such an error,
and continues to close the rest of the cursors and database handles.
</p>
<p>
Although closing a database handle will close any open cursors, it
is recommended that applications explicitly close all their
<aclass="link"href="dbc.html"title="Chapter3. The DBcursor Handle">DBcursor</a> handles
before closing the database. The reason why is that when
the cursor is explicitly closed, the memory allocated for it is
reclaimed; however, this will <spanclass="emphasis"><em>not</em></span> happen if
you close a database while cursors are still opened.
</p>
<p>
The same rule, for the same reasons, hold true for
<aclass="link"href="txn.html"title="Chapter12. The DB_TXN Handle">DB_TXN</a>
handles. Simply make sure you close all your transaction handles
before closing your database handle.
</p>
<p>
Because key/data pairs are cached in memory, applications should
make a point to always either close database handles or sync their
data to disk (using the <aclass="xref"href="dbsync.html"title="DB->sync()">DB->sync()</a>
method) before exiting, to ensure that any data cached in main memory are
reflected in the underlying file system.
</p>
<p>
When called on a database that is the primary database for a secondary
index, the primary database should be closed only after all secondary
indices referencing it have been closed.
</p>
<p>
When multiple threads are using the <aclass="link"href="db.html"title="Chapter2. The DB Handle">DB</a>
concurrently, only a single thread may call the <codeclass="methodname">DB->close()</code> method.
</p>
<p>
The <aclass="link"href="db.html"title="Chapter2. The DB Handle">DB</a> handle may not be
accessed again after <codeclass="methodname">DB->close()</code> is called, regardless of its return.
</p>
<p>
If you do not close the <aclass="link"href="db.html"title="Chapter2. The DB Handle">DB</a> handle explicitly, it will be closed when
the environment handle that owns the <aclass="link"href="db.html"title="Chapter2. The DB Handle">DB</a> handle is closed.
</p>
<p>
The <codeclass="methodname">DB->close()</code><span>
<span>
method returns a non-zero error value on failure and 0 on success.
</span>
</span>
The error values that <codeclass="methodname">DB->close()</code> method returns include the error values of <codeclass="methodname">DBcursor->close()</code> and the following: