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