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>Db::close()</title> 7 <link rel="stylesheet" href="apiReference.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" /> 10 <link rel="up" href="db.html" title="Chapter 2. The Db Handle" /> 11 <link rel="prev" href="dbassociate_foreign.html" title="Db::associate_foreign()" /> 12 <link rel="next" href="dbcreate.html" title="Db" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Db::close()</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbassociate_foreign.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 2. 23 The Db Handle 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="dbcreate.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="dbclose"></a>Db::close()</h2> 35 </div> 36 </div> 37 </div> 38 <pre class="programlisting">#include <db_cxx.h> 39 40int 41Db::close(u_int32_t flags);</pre> 42 <p> 43 The <code class="methodname">Db::close()</code> method flushes any cached database information to 44 disk, closes any open cursors, frees any allocated resources, and 45 closes any underlying files. 46 </p> 47 <p> 48 Although closing a database handle will close any open cursors, it 49 is recommended that applications explicitly close all their 50 <a class="link" href="dbc.html" title="Chapter 3. The Dbc Handle">Dbc</a> handles 51 before closing the database. The reason why is that when 52 the cursor is explicitly closed, the memory allocated for it is 53 reclaimed; however, this will <span class="emphasis"><em>not</em></span> happen if 54 you close a database while cursors are still opened. 55 </p> 56 <p> 57 The same rule, for the same reasons, hold true for 58 <a class="link" href="txn.html" title="Chapter 13. The DbTxn Handle">DbTxn</a> 59 handles. Simply make sure you close all your transaction handles 60 before closing your database handle. 61 </p> 62 <p> 63 Because key/data pairs are cached in memory, applications should 64 make a point to always either close database handles or sync their 65 data to disk (using the <a class="xref" href="dbsync.html" title="Db::sync()">Db::sync()</a> 66 method) before exiting, to ensure that any data cached in main memory are 67 reflected in the underlying file system. 68 </p> 69 <p> 70 When called on a database that is the primary database for a secondary 71 index, the primary database should be closed only after all secondary 72 indices referencing it have been closed. 73 </p> 74 <p> 75 When multiple threads are using the <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> 76 concurrently, only a single thread may call the <code class="methodname">Db::close()</code> method. 77 </p> 78 <p> 79 The <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> handle may not be 80 accessed again after <code class="methodname">Db::close()</code> is called, regardless of its return. 81 </p> 82 <p> 83 The <code class="methodname">Db::close()</code> <span> 84 85 <span> 86 method either returns a non-zero error value or throws an 87 exception that encapsulates a non-zero error value on 88 failure, and returns 0 on success. 89 </span> 90 </span> 91 </p> 92 <div class="sect2" lang="en" xml:lang="en"> 93 <div class="titlepage"> 94 <div> 95 <div> 96 <h3 class="title"><a id="id1630309"></a>Parameters</h3> 97 </div> 98 </div> 99 </div> 100 <div class="sect3" lang="en" xml:lang="en"> 101 <div class="titlepage"> 102 <div> 103 <div> 104 <h4 class="title"><a id="id1631831"></a>flags</h4> 105 </div> 106 </div> 107 </div> 108 <p> 109 The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 110 or be set to the following value: 111 </p> 112 <div class="itemizedlist"> 113 <ul type="disc"> 114 <li> 115 <p><a id="dbclose_DB_NOSYNC"></a> 116 <code class="literal">DB_NOSYNC</code> 117 </p> 118 <p> 119 Do not flush cached information to disk. This flag is a 120 dangerous option. It should be set only if the application is doing 121 logging (with transactions) so that the database is recoverable after 122 a system or application crash, or if the database is always generated 123 from scratch after any system or application crash. 124 </p> 125 <p> 126 <span class="bold"><strong>It is important to understand that flushing 127 cached information to disk only minimizes the window of opportunity 128 for corrupted data.</strong></span> Although unlikely, it is possible for 129 database corruption to happen if a system or application crash occurs 130 while writing data to the database. To ensure that database 131 corruption never occurs, applications must either: use transactions 132 and logging with automatic recovery; use logging and 133 application-specific recovery; or edit a copy of the database, and 134 once all applications using the database have successfully called 135 <code class="methodname">Db::close()</code>, atomically replace the original database with the 136 updated copy. 137 </p> 138 <p> 139 Note that this flag only works when the database 140 has been opened using an environment. 141 </p> 142 </li> 143 </ul> 144 </div> 145 </div> 146 </div> 147 <div class="sect2" lang="en" xml:lang="en"> 148 <div class="titlepage"> 149 <div> 150 <div> 151 <h3 class="title"><a id="id1631670"></a>Errors</h3> 152 </div> 153 </div> 154 </div> 155 <p> 156 The <code class="methodname">Db::close()</code> <span> 157 158 <span> 159 method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> 160 exception, encapsulating one of the following non-zero errors, or return one 161 of the following non-zero errors: 162 </span> 163 </span> 164 </p> 165 <div class="sect3" lang="en" xml:lang="en"> 166 <div class="titlepage"> 167 <div> 168 <div> 169 <h4 class="title"><a id="id1629693"></a>EINVAL</h4> 170 </div> 171 </div> 172 </div> 173 <p> 174 An invalid flag value or parameter was specified. 175 </p> 176 </div> 177 </div> 178 <div class="sect2" lang="en" xml:lang="en"> 179 <div class="titlepage"> 180 <div> 181 <div> 182 <h3 class="title"><a id="id1631850"></a>Class</h3> 183 </div> 184 </div> 185 </div> 186 <p> 187 <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> 188 </p> 189 </div> 190 <div class="sect2" lang="en" xml:lang="en"> 191 <div class="titlepage"> 192 <div> 193 <div> 194 <h3 class="title"><a id="id1631709"></a>See Also</h3> 195 </div> 196 </div> 197 </div> 198 <p> 199 <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> 200 </p> 201 </div> 202 </div> 203 <div class="navfooter"> 204 <hr /> 205 <table width="100%" summary="Navigation footer"> 206 <tr> 207 <td width="40%" align="left"><a accesskey="p" href="dbassociate_foreign.html">Prev</a> </td> 208 <td width="20%" align="center"> 209 <a accesskey="u" href="db.html">Up</a> 210 </td> 211 <td width="40%" align="right"> <a accesskey="n" href="dbcreate.html">Next</a></td> 212 </tr> 213 <tr> 214 <td width="40%" align="left" valign="top">Db::associate_foreign() </td> 215 <td width="20%" align="center"> 216 <a accesskey="h" href="index.html">Home</a> 217 </td> 218 <td width="40%" align="right" valign="top"> Db</td> 219 </tr> 220 </table> 221 </div> 222 </body> 223</html> 224