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.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DB.html" title="Chapter 2. Databases" /> 11 <link rel="prev" 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> 36 <p> 37 Once you are done using the database, you must close it. You use the 38 39 <code class="methodname">Db::close()</code> 40 method to do this. 41 </p> 42 <p> 43 Closing a database causes it to become unusable until it is opened 44 again. Note that you should make sure that any open cursors are closed 45 before closing your database. Active cursors during a database 46 close can cause unexpected results, especially if any of those cursors are 47 writing to the database. You should always make sure that all your 48 database accesses have completed before closing your database. 49 </p> 50 <p> 51 Cursors are described in <a class="xref" href="Cursors.html" title="Chapter 4. Using Cursors">Using Cursors</a> later in this manual. 52 </p> 53 <p> 54 Be aware that when you close the last open handle 55 for a database, then by default its cache is flushed to disk. 56 This means that any information that has 57 been modified in the cache is guaranteed to be written to disk when the 58 last handle is closed. You can manually perform this operation using 59 the 60 61 <code class="methodname">Db::sync()</code> 62 63 method, but for normal shutdown operations it is not necessary. 64 For more information about syncing your cache, see 65 <a class="xref" href="usingDbt.html#datapersist" title="Data Persistence">Data Persistence</a>. 66 </p> 67 <p>The following code fragment illustrates a database close:</p> 68 <a id="cxx_db2"></a> 69 <pre class="programlisting">#include <db_cxx.h> 70 71... 72 73Db db(NULL, 0); 74 75 // Database open and access operations happen here. 76 77try { 78 // Close the database 79 db.close(0); 80// DbException is not subclassed from std::exception, so 81// need to catch both of these. 82} catch(DbException &e) { 83 // Error handling code goes here 84} catch(std::exception &e) { 85 // Error handling code goes here 86} </pre> 87 </div> 88 <div class="navfooter"> 89 <hr /> 90 <table width="100%" summary="Navigation footer"> 91 <tr> 92 <td width="40%" align="left"><a accesskey="p" href="DB.html">Prev</a> </td> 93 <td width="20%" align="center"> 94 <a accesskey="u" href="DB.html">Up</a> 95 </td> 96 <td width="40%" align="right"> <a accesskey="n" href="DBOpenFlags.html">Next</a></td> 97 </tr> 98 <tr> 99 <td width="40%" align="left" valign="top">Chapter 2. Databases </td> 100 <td width="20%" align="center"> 101 <a accesskey="h" href="index.html">Home</a> 102 </td> 103 <td width="40%" align="right" valign="top"> Database Open Flags</td> 104 </tr> 105 </table> 106 </div> 107 </body> 108</html> 109