2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Introduction</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Application Specific Logging and Recovery</dl></h3></td>
14
<td align=right><a href="../../ref/xa/faq.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/apprec/def.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Introduction</h1>
18
<p>It is possible to use the Locking, Logging and Transaction subsystems
19
of Berkeley DB to provide transaction semantics on objects other than those
20
described by the Berkeley DB access methods. In these cases, the application
21
will need application-specific logging and recovery functions.
22
<p>For example, consider an application that provides transaction semantics
23
on data stored in plain text files accessed using the POSIX read and
24
write system calls. The read and write operations for which transaction
25
protection is desired will be bracketed by calls to the standard Berkeley DB
26
transactional interfaces, <a href="../../api_c/txn_begin.html">DB_ENV->txn_begin</a> and <a href="../../api_c/txn_commit.html">DB_TXN->commit</a>, and
27
the transaction's locker ID will be used to acquire relevant read and
29
<p>Before data is accessed, the application must make a call to the lock
30
manager, <a href="../../api_c/lock_get.html">DB_ENV->lock_get</a>, for a lock of the appropriate type (for
31
example, read) on the object being locked. The object might be a page
32
in the file, a byte, a range of bytes, or some key. It is up to the
33
application to ensure that appropriate locks are acquired. Before a
34
write is performed, the application should acquire a write lock on the
35
object by making an appropriate call to the lock manager,
36
<a href="../../api_c/lock_get.html">DB_ENV->lock_get</a>. Then, the application should make a call to the log
37
manager, <a href="../../api_c/log_put.html">DB_ENV->log_put</a>, to record enough information to redo the
38
operation in case of failure after commit and to undo the operation in
40
<p>When designing applications that will use the log subsystem, it is
41
important to remember that the application is responsible for providing
42
any necessary structure to the log record. For example, the application
43
must understand what part of the log record is an operation code, what
44
part identifies the file being modified, what part is redo information,
45
and what part is undo information.
46
<p>After the log message is written, the application may issue the write
47
system call. After all requests are issued, the application may call
48
<a href="../../api_c/txn_commit.html">DB_TXN->commit</a>. When <a href="../../api_c/txn_commit.html">DB_TXN->commit</a> returns, the caller is
49
guaranteed that all necessary log writes have been written to disk.
50
<p>At any time before issuing a <a href="../../api_c/txn_commit.html">DB_TXN->commit</a>,
51
the application may call <a href="../../api_c/txn_abort.html">DB_TXN->abort</a>, which will
52
result in restoration of the database to a consistent pretransaction
53
state. (The application may specify its own recovery function for this
54
purpose using the <a href="../../api_c/env_set_app_dispatch.html">DB_ENV->set_app_dispatch</a> method. The recovery
55
function must be able to either reapply or undo the update depending on
56
the context, for each different type of log record.)
57
<p>If the application crashes, the recovery process uses the log to restore
58
the database to a consistent state.
59
<p>Berkeley DB includes tools to assist in the development of application-specific
60
logging and recovery. Specifically, given a description of information
61
to be logged in a family of log records, these tools will automatically
62
create log-writing functions (functions that marshall their arguments
63
into a single log record), log-reading functions (functions that read
64
a log record and unmarshall it into a structure containing fields that
65
map into the arguments written to the log), log-printing functions
66
(functions that print the contents of a log record for debugging), and
67
templates for recovery functions (functions that review log records
68
during transaction abort or recovery). The tools and generated code
69
are C-language and POSIX-system based, but the generated code should be
70
usable on any system, not just POSIX systems.
71
<p>A sample application that does application-specific recovery is included
72
in the Berkeley DB distribution, in the directory <b>examples_c/ex_apprec</b>.
73
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/xa/faq.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/apprec/def.html"><img src="../../images/next.gif" alt="Next"></a>
75
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>