summaryrefslogtreecommitdiff
path: root/db/docs/ref/transapp/faq.html
blob: 1a94344a66573ca429f049688479dedc05702f66 (plain)
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
78
79
80
81
82
83
<!--Id: faq.so,v 10.5 2002/05/14 15:01:45 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: Transaction FAQ</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>
<a name="2"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
<td align=right><a href="../../ref/transapp/throughput.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/rep/intro.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h1 align=center>Transaction FAQ</h1>
<p><ol>
<p><li><b>What should a transactional program do when an error occurs?</b>
<p>Any time an error occurs, such that a transactionally protected set of
operations cannot complete successfully, the transaction must be
aborted.  While deadlock is by far the most common of these errors,
there are other possibilities; for example, running out of disk space
for the filesystem.  In Berkeley DB transactional applications, there are
three classes of error returns: "expected" errors, "unexpected but
recoverable" errors, and a single "unrecoverable" error.  Expected
errors are errors like <a href="../../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>, which indicates that a
searched-for key item is not present in the database.  Applications may
want to explicitly test for and handle this error, or, in the case where
the absence of a key implies the enclosing transaction should fail,
simply call <a href="../../api_c/txn_abort.html">DB_TXN-&gt;abort</a>.  Unexpected but recoverable errors are
errors like <a href="../../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a>, which indicates that an operation
has been selected to resolve a deadlock, or a system error such as EIO,
which likely indicates that the filesystem has no available disk space.
Applications must immediately call <a href="../../api_c/txn_abort.html">DB_TXN-&gt;abort</a> when these returns
occur, as it is not possible to proceed otherwise.  The only
unrecoverable error is <a href="../../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a>, which indicates that the
system must stop and recovery must be run.
<p><li><b>How can hot backups work?  Can't you get an inconsistent picture
of the database when you copy it?</b>
<p>First, Berkeley DB is based on the technique of "write-ahead logging", which
means that before any change is made to a database, a log record is
written that describes the change.  Further, Berkeley DB guarantees that the
log record that describes the change will always be written to stable
storage (that is, disk) before the database page where the change was
made is written to stable storage.  Because of this guarantee, we know
that any change made to a database will appear either in just a log
file, or both the database and a log file, but never in just the
database.
<p>Second, you can always create a consistent and correct database based
on the log files and the databases from a database environment.  So,
during a hot backup, we first make a copy of the databases and then a
copy of the log files.  The tricky part is that there may be pages in
the database that are related for which we won't get a consistent
picture during this copy.  For example, let's say that we copy pages
1-4 of the database, and then are swapped out.  For whatever reason
(perhaps because we needed to flush pages from the cache, or because of
a checkpoint), the database pages 1 and 5 are written.  Then, the hot
backup process is re-scheduled, and it copies page 5.  Obviously, we
have an inconsistent database snapshot, because we have a copy of page
1 from before it was written by the other thread of control, and a copy
of page 5 after it was written by the other thread.  What makes this
work is the order of operations in a hot backup.  Because of the
write-ahead logging guarantees, we know that any page written to the
database will first be referenced in the log.  If we copy the database
first, then we can also know that any inconsistency in the database will
be described in the log files, and so we know that we can fix everything
up during recovery.
<p><li><b>How can I move a database from one transactional environment
into another?</b>
<p>Because database pages contain references to log records, databases
cannot be simply moved into different database environments.  To move
a database into a different environment, dump and reload the database
before moving it.  If the database is too large to dump and reload, the
database may be prepared in place by setting the first eight bytes of
each page in the file to 0.
</ol>
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/throughput.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/rep/intro.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>