Debian Codegraph @

docs/gsg_txn/CXX/usingtxns.html

raw
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>Chapter 3. Transaction Basics</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 Transaction Processing" />
10 <link rel="up" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
11 <link rel="prev" href="envopen.html" title="Opening a Transactional Environment and Database" />
12 <link rel="next" href="nodurabletxn.html" title="Non-Durable Transactions" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Chapter 3. Transaction Basics</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="envopen.html">Prev</a> </td>
25 <th width="60%" align="center"> </th>
26 <td width="20%" align="right"> <a accesskey="n" href="nodurabletxn.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="chapter" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title"><a id="usingtxns"></a>Chapter 3. Transaction Basics</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <p>
41 <b>Table of Contents</b>
42 </p>
43 <dl>
44 <dt>
45 <span class="sect1">
46 <a href="usingtxns.html#commitresults">Committing a Transaction</a>
47 </span>
48 </dt>
49 <dt>
50 <span class="sect1">
51 <a href="nodurabletxn.html">Non-Durable Transactions</a>
52 </span>
53 </dt>
54 <dt>
55 <span class="sect1">
56 <a href="abortresults.html">Aborting a Transaction</a>
57 </span>
58 </dt>
59 <dt>
60 <span class="sect1">
61 <a href="autocommit.html">Auto Commit</a>
62 </span>
63 </dt>
64 <dt>
65 <span class="sect1">
66 <a href="nestedtxn.html">Nested Transactions</a>
67 </span>
68 </dt>
69 <dt>
70 <span class="sect1">
71 <a href="txncursor.html">Transactional Cursors</a>
72 </span>
73 </dt>
74 <dt>
75 <span class="sect1">
76 <a href="txnindices.html">Secondary Indices with Transaction Applications</a>
77 </span>
78 </dt>
79 <dt>
80 <span class="sect1">
81 <a href="maxtxns.html">Configuring the Transaction Subsystem</a>
82 </span>
83 </dt>
84 </dl>
85 </div>
86 <p>
87 Once you have enabled transactions for your environment and your databases,
88 you can use them to protect your database operations. You do this by
89 acquiring a transaction handle and then using that handle for any
90 database operation that you want to participate in that transaction.
91 </p>
92 <p>
93 You obtain a transaction handle using the
94
95 <span><code class="methodname">DbEnv::txn_begin()</code> method.</span>
96
97
98
99 </p>
100 <p>
101 Once you have completed all of the operations that you want to include
102 in the transaction, you must commit the transaction using the
103
104 <span><code class="methodname">DbTxn::commit()</code> method.</span>
105
106
107
108
109 </p>
110 <p>
111 If, for any reason, you want to abandon the transaction, you abort
112 it using
113
114 <span><code class="methodname">DbTxn::abort()</code>.</span>
115
116
117
118
119
120 </p>
121 <p>
122 Any transaction handle that has been committed or aborted can no longer
123 be used by your application.
124 </p>
125 <p>
126 Finally, you must make sure that all transaction handles are either
127 committed or aborted before closing your databases and environment.
128 </p>
129 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
130 <h3 class="title">Note</h3>
131 <p>
132 If you only want to transaction protect a single database write operation, you can use auto commit to
133 perform the transaction administration. When you use auto commit, you do not need an explicit transaction
134 handle. See <a class="xref" href="autocommit.html" title="Auto Commit">Auto Commit</a> for more information.
135 </p>
136 </div>
137 <p>
138 For example, the following example opens a transactional-enabled environment and
139 database, obtains a transaction handle, and then performs a write
140 operation under its protection. In the event of any failure in the
141 write operation, the transaction is aborted and the database is left in a
142 state as if no operations had ever been attempted in the first place.
143 </p>
144 <pre class="programlisting">#include "db_cxx.h"
145
146 ...
147
148 int main(void)
149 {
150 u_int32_t env_flags = DB_CREATE | // If the environment does not
151 // exist, create it.
152 DB_INIT_LOCK | // Initialize locking
153 DB_INIT_LOG | // Initialize logging
154 DB_INIT_MPOOL | // Initialize the cache
155 DB_INIT_TXN; // Initialize transactions
156
157 u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT;
158 Db *dbp = NULL;
159 const char *file_name = "mydb.db";
160 const char *keystr ="thekey";
161 const char *datastr = "thedata";
162
163 std::string envHome("/export1/testEnv");
164 DbEnv myEnv(0);
165
166 try {
167
168 myEnv.open(envHome.c_str(), env_flags, 0);
169 dbp = new Db(&amp;myEnv, 0);
170
171 // Open the database. Note that we are using auto commit for
172 // the open, so the database is able to support transactions.
173 dbp-&gt;open(NULL, // Txn pointer
174 file_name, // File name
175 NULL, // Logical db name
176 DB_BTREE, // Database type (using btree)
177 db_flags, // Open flags
178 0); // File mode. Using defaults
179
180 Dbt key, data;
181 key.set_data(keystr);
182 key.set_size((strlen(keystr) + 1) * sizeof(char));
183 key.set_data(datastr);
184 key.set_size((strlen(datastr) + 1) * sizeof(char));
185
186 DbTxn *txn = NULL;
187 myEnv.txn_begin(NULL, &amp;txn, 0);
188 try {
189 db-&gt;put(txn, &amp;key, &amp;data, 0);
190 txn-&gt;commit(0);
191 } catch (DbException &amp;e) {
192 std::cerr &lt;&lt; "Error in transaction: "
193 &lt;&lt; e.what() &lt;&lt; std::endl;
194 txn-&gt;abort();
195 }
196
197 } catch(DbException &amp;e) {
198 std::cerr &lt;&lt; "Error opening database and environment: "
199 &lt;&lt; file_name &lt;&lt; ", "
200 &lt;&lt; envHome &lt;&lt; std::endl;
201 std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
202 }
203
204 try {
205 if (dbp != NULL)
206 dbp-&gt;close(0);
207 myEnv.close(0);
208 } catch(DbException &amp;e) {
209 std::cerr &lt;&lt; "Error closing database and environment: "
210 &lt;&lt; file_name &lt;&lt; ", "
211 &lt;&lt; envHome &lt;&lt; std::endl;
212 std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
213 return (EXIT_FAILURE);
214 }
215
216 return (EXIT_SUCCESS);
217 } </pre>
218 <div class="sect1" lang="en" xml:lang="en">
219 <div class="titlepage">
220 <div>
221 <div>
222 <h2 class="title" style="clear: both"><a id="commitresults"></a>Committing a Transaction</h2>
223 </div>
224 </div>
225 </div>
226 <p>
227 In order to fully understand what is happening when you commit
228 a transaction, you must first understand a little about what
229 DB is doing with
230
231 <span>
232 the logging subsystem.
233 </span>
234
235
236
237 Logging causes all database write operations to be identified in
238
239 <span>logs, and by default these
240 logs are backed by files on disk. These logs are used to restore your databases
241
242 </span>
243
244
245
246 in the event of a system or application failure, so by performing
247 logging, DB ensures the integrity of your data.
248 </p>
249 <p>
250 Moreover, DB performs <span class="emphasis"><em>write-ahead</em></span>
251 logging. This means that information is written to the logs
252 <span class="emphasis"><em>before</em></span> the actual database
253
254 is changed.
255 This means that all write activity performed under the
256 protection of the transaction is noted in the log before
257 the transaction is committed. Be aware, however, that database
258 maintains logs in-memory. If you are backing your logs on
259 disk, the log information will eventually be written to the log
260 files, but while the transaction is on-going the log data may be
261 held only in memory.
262 </p>
263 <p>
264 When you commit a transaction, the following occurs:
265 </p>
266 <div class="itemizedlist">
267 <ul type="disc">
268 <li>
269 <p>
270 A commit record is written to the log. This
271 indicates that the modifications made by the
272 transaction are now permanent. By default, this write is performed synchronously to disk so the
273 commit record arrives in the log files before any other actions are taken.
274 </p>
275 </li>
276 <li>
277 <p>
278 Any log information held in memory is (by default)
279 synchronously written to disk. Note that this requirement can be
280 relaxed, depending on the type of commit you perform.
281 See <a class="xref" href="nodurabletxn.html" title="Non-Durable Transactions">Non-Durable Transactions</a> for
282 more information.
283 <span>Also, if you are
284 maintaining your logs entirely in-memory, then this
285 step will of course not be taken. To configure your
286 logging system for in-memory usage, see
287 <a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>.
288 </span>
289 </p>
290 </li>
291 <li>
292 <p>
293 All locks held by the transaction are released. This means
294 that read operations performed by other transactions or
295 threads of control can now see the modifications without
296 resorting to uncommitted reads (see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a> for more information).
297 </p>
298 </li>
299 </ul>
300 </div>
301 <p>
302 To commit a transaction, you simply call
303
304 <span><code class="methodname">DbTxn::commit()</code>.</span>
305
306
307
308 </p>
309 <p>
310 Notice that committing a transaction does not necessarily cause data
311 modified in your memory cache to be written to the files
312 backing your databases on disk. Dirtied database pages are written
313 for a number of reasons, but a transactional
314 commit is not one of them. The following are the things that can cause a dirtied
315 database page to be written to the backing database file:
316 </p>
317 <div class="itemizedlist">
318 <ul type="disc">
319 <li>
320 <p>
321 Checkpoints.
322 </p>
323 <p>
324 Checkpoints cause all dirtied pages currently existing
325 in the cache to be written to disk, and a checkpoint
326 record is then written to the logs. You can run checkpoints
327 explicitly. For more information on checkpoints,
328 see <a class="xref" href="filemanagement.html#checkpoints" title="Checkpoints">Checkpoints</a>.
329 </p>
330 </li>
331 <li>
332 <p>
333 Cache is full.
334 </p>
335 <p>
336 If the in-memory cache fills up, then dirtied pages
337 might be written to disk in order to free up space for other
338 pages that your application needs to use. Note that if
339 dirtied pages are written to the database files, then
340 any log records that describe how those pages were
341 dirtied are written to disk before the database
342 pages are written.
343 </p>
344 </li>
345 </ul>
346 </div>
347 <p>
348 Be aware that because your transaction commit caused database
349
350 modifications recorded in your logs to be forced to disk, your modifications
351 are by default "persistent" in that they can be recovered in the event of
352 an application or system failure. However, recovery time is
353 gated by how much data has been modified since the last
354 checkpoint, so for applications that perform a lot of writes,
355 you may want to run a checkpoint with some frequency.
356 </p>
357 <p>
358 Note that once you have committed a transaction, the transaction
359 handle that you used for the transaction is no longer valid. To
360 perform database activities under the control of a new
361 transaction, you must obtain a fresh transaction handle.
362 </p>
363 </div>
364 </div>
365 <div class="navfooter">
366 <hr />
367 <table width="100%" summary="Navigation footer">
368 <tr>
369 <td width="40%" align="left"><a accesskey="p" href="envopen.html">Prev</a> </td>
370 <td width="20%" align="center"> </td>
371 <td width="40%" align="right"> <a accesskey="n" href="nodurabletxn.html">Next</a></td>
372 </tr>
373 <tr>
374 <td width="40%" align="left" valign="top">Opening a Transactional Environment and
375 <span>Database</span>
376
377
378  </td>
379 <td width="20%" align="center">
380 <a accesskey="h" href="index.html">Home</a>
381 </td>
382 <td width="40%" align="right" valign="top"> Non-Durable Transactions</td>
383 </tr>
384 </table>
385 </div>
386 </body>
387 </html>