| 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 14. Application Specific Logging and Recovery</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="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 11 | <link rel="prev" href="xa_faq.html" title="XA: Frequently Asked Questions" /> |
| 12 | <link rel="next" href="apprec_def.html" title="Defining application-specific log records" /> |
| 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 14. |
| 22 | Application Specific Logging and Recovery |
| 23 | </th> |
| 24 | </tr> |
| 25 | <tr> |
| 26 | <td width="20%" align="left"><a accesskey="p" href="xa_faq.html">Prev</a> </td> |
| 27 | <th width="60%" align="center"> </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="apprec_def.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="chapter" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title"><a id="apprec"></a>Chapter 14. |
| 38 | Application Specific Logging and Recovery |
| 39 | </h2> |
| 40 | </div> |
| 41 | </div> |
| 42 | </div> |
| 43 | <div class="toc"> |
| 44 | <p> |
| 45 | <b>Table of Contents</b> |
| 46 | </p> |
| 47 | <dl> |
| 48 | <dt> |
| 49 | <span class="sect1"> |
| 50 | <a href="apprec.html#apprec_intro">Introduction to application specific logging and recovery</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect1"> |
| 55 | <a href="apprec_def.html">Defining application-specific log records</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | <dt> |
| 59 | <span class="sect1"> |
| 60 | <a href="apprec_auto.html">Automatically generated functions</a> |
| 61 | </span> |
| 62 | </dt> |
| 63 | <dt> |
| 64 | <span class="sect1"> |
| 65 | <a href="apprec_config.html">Application configuration</a> |
| 66 | </span> |
| 67 | </dt> |
| 68 | </dl> |
| 69 | </div> |
| 70 | <div class="sect1" lang="en" xml:lang="en"> |
| 71 | <div class="titlepage"> |
| 72 | <div> |
| 73 | <div> |
| 74 | <h2 class="title" style="clear: both"><a id="apprec_intro"></a>Introduction to application specific logging and recovery</h2> |
| 75 | </div> |
| 76 | </div> |
| 77 | </div> |
| 78 | <p>It is possible to use the Locking, Logging and Transaction subsystems |
| 79 | of Berkeley DB to provide transaction semantics on objects other than those |
| 80 | described by the Berkeley DB access methods. In these cases, the application |
| 81 | will need application-specific logging and recovery functions.</p> |
| 82 | <p>For example, consider an application that provides transaction semantics |
| 83 | on data stored in plain text files accessed using the POSIX read and |
| 84 | write system calls. The read and write operations for which transaction |
| 85 | protection is desired will be bracketed by calls to the standard Berkeley DB |
| 86 | transactional interfaces, <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV->txn_begin()</a> and <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a>, and |
| 87 | the transaction's locker ID will be used to acquire relevant read and |
| 88 | write locks.</p> |
| 89 | <p>Before data is accessed, the application must make a call to the lock |
| 90 | manager, <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a>, for a lock of the appropriate type (for |
| 91 | example, read) on the object being locked. The object might be a page |
| 92 | in the file, a byte, a range of bytes, or some key. It is up to the |
| 93 | application to ensure that appropriate locks are acquired. Before a |
| 94 | write is performed, the application should acquire a write lock on the |
| 95 | object by making an appropriate call to the lock manager, |
| 96 | <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a>. Then, the application should make a call to the log |
| 97 | manager, via the automatically-generated log-writing function described |
| 98 | as follows. This record should contain enough information to redo the |
| 99 | operation in case of failure after commit and to undo the operation in |
| 100 | case of abort.</p> |
| 101 | <p>When designing applications that will use the log subsystem, it is |
| 102 | important to remember that the application is responsible for providing |
| 103 | any necessary structure to the log record. For example, the application |
| 104 | must understand what part of the log record is an operation code, what |
| 105 | part identifies the file being modified, what part is redo information, |
| 106 | and what part is undo information.</p> |
| 107 | <p>After the log message is written, the application may issue the write |
| 108 | system call. After all requests are issued, the application may call |
| 109 | <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a>. When <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a> returns, the caller is |
| 110 | guaranteed that all necessary log writes have been written to disk.</p> |
| 111 | <p>At any time before issuing a <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a>, the application may |
| 112 | call <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN->abort()</a>, which will result in restoration of the database |
| 113 | to a consistent pretransaction state. (The application may specify its |
| 114 | own recovery function for this purpose using the |
| 115 | <a href="../api_reference/C/envset_app_dispatch.html" class="olink">DB_ENV->set_app_dispatch()</a> method. The recovery function must be able to |
| 116 | either reapply or undo the update depending on the context, for each |
| 117 | different type of log record. The recovery functions must not use Berkeley DB |
| 118 | methods to access data in the environment as there is no way to |
| 119 | coordinate these accesses with either the aborting transaction or the |
| 120 | updates done by recovery or replication.)</p> |
| 121 | <p>If the application crashes, the recovery process uses the log to restore |
| 122 | the database to a consistent state.</p> |
| 123 | <p>Berkeley DB includes tools to assist in the development of application-specific |
| 124 | logging and recovery. Specifically, given a description of information |
| 125 | to be logged in a family of log records, these tools will automatically |
| 126 | create log-writing functions (functions that marshall their arguments |
| 127 | into a single log record), log-reading functions (functions that read |
| 128 | a log record and unmarshall it into a structure containing fields that |
| 129 | map into the arguments written to the log), log-printing functions |
| 130 | (functions that print the contents of a log record for debugging), and |
| 131 | templates for recovery functions (functions that review log records |
| 132 | during transaction abort or recovery). The tools and generated code |
| 133 | are C-language and POSIX-system based, but the generated code should be |
| 134 | usable on any system, not just POSIX systems.</p> |
| 135 | <p>A sample application that does application-specific recovery is included |
| 136 | in the Berkeley DB distribution, in the directory <code class="filename">examples_c/ex_apprec</code>.</p> |
| 137 | </div> |
| 138 | </div> |
| 139 | <div class="navfooter"> |
| 140 | <hr /> |
| 141 | <table width="100%" summary="Navigation footer"> |
| 142 | <tr> |
| 143 | <td width="40%" align="left"><a accesskey="p" href="xa_faq.html">Prev</a> </td> |
| 144 | <td width="20%" align="center"> </td> |
| 145 | <td width="40%" align="right"> <a accesskey="n" href="apprec_def.html">Next</a></td> |
| 146 | </tr> |
| 147 | <tr> |
| 148 | <td width="40%" align="left" valign="top">XA: Frequently Asked Questions </td> |
| 149 | <td width="20%" align="center"> |
| 150 | <a accesskey="h" href="index.html">Home</a> |
| 151 | </td> |
| 152 | <td width="40%" align="right" valign="top"> Defining application-specific log records</td> |
| 153 | </tr> |
| 154 | </table> |
| 155 | </div> |
| 156 | </body> |
| 157 | </html> |