1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>Closing Databases</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
9    <link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="DB.html" title="Chapter 2. Databases" />
11    <link rel="previous" href="DB.html" title="Chapter 2. Databases" />
12    <link rel="next" href="DBOpenFlags.html" title="Database Open Flags" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Closing Databases</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="DB.html">Prev</a> </td>
22          <th width="60%" align="center">Chapter 2. Databases</th>
23          <td width="20%" align="right"> <a accesskey="n" href="DBOpenFlags.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="coredbclose"></a>Closing Databases</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        Once you are done using the database, you must close it. You use the
39        
40        <tt class="methodname">Db::close()</tt>
41        method to do this.
42    </p>
43      <p>
44        Closing a database causes it to become unusable until it is opened
45        again. Note that you should make sure that any open cursors are closed
46        before closing your database.  Active cursors during a database
47        close can cause unexpected results, especially if any of those cursors are
48        writing to the database. You should always make sure that all your
49        database accesses have completed before closing your database.
50    </p>
51      <p>
52        Cursors are described in <a href="Cursors.html">Using Cursors</a> later in this manual.
53    </p>
54      <p>
55        Be aware that when you close the last open handle 
56        for a database, then by default its cache is flushed to disk.
57        This means that any information that has
58        been modified in the cache is guaranteed to be written to disk when the
59        last handle is closed. You can manually perform this operation using
60        the 
61        
62        <tt class="methodname">Db::sync()</tt>
63        
64        method, but for normal shutdown operations it is not necessary.
65        For more information about syncing your cache, see 
66        <a href="usingDbt.html#datapersist">Data Persistence</a>.
67     </p>
68      <p>The following code fragment illustrates a database close:</p>
69      <a id="cxx_db2"></a>
70      <pre class="programlisting">#include &lt;db_cxx.h&gt;
71
72...
73
74Db db(NULL, 0);
75
76 // Database open and access operations happen here.
77
78try {
79    // Close the database
80    db.close(0);
81// DbException is not subclassed from std::exception, so
82// need to catch both of these.
83} catch(DbException &amp;e) {
84    // Error handling code goes here    
85} catch(std::exception &amp;e) {
86    // Error handling code goes here
87} </pre>
88    </div>
89    <div class="navfooter">
90      <hr />
91      <table width="100%" summary="Navigation footer">
92        <tr>
93          <td width="40%" align="left"><a accesskey="p" href="DB.html">Prev</a> </td>
94          <td width="20%" align="center">
95            <a accesskey="u" href="DB.html">Up</a>
96          </td>
97          <td width="40%" align="right"> <a accesskey="n" href="DBOpenFlags.html">Next</a></td>
98        </tr>
99        <tr>
100          <td width="40%" align="left" valign="top">Chapter 2. Databases </td>
101          <td width="20%" align="center">
102            <a accesskey="h" href="index.html">Home</a>
103          </td>
104          <td width="40%" align="right" valign="top"> Database Open Flags</td>
105        </tr>
106      </table>
107    </div>
108  </body>
109</html>
110