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: Nested transactions</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
<a name="2"><!--meow--></a>
13
<table width="100%"><tr valign=top>
14
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
15
<td align=right><a href="../../ref/transapp/cursor.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/admin.html"><img src="../../images/next.gif" alt="Next"></a>
18
<h1 align=center>Nested transactions</h1>
19
<p>Berkeley DB provides support for nested transactions. Nested transactions
20
allow an application to decompose a large or long-running transaction
21
into smaller units that may be independently aborted.
22
<p>Normally, when beginning a transaction, the application will pass a NULL
23
value for the parent argument to <a href="../../api_c/txn_begin.html">DB_ENV->txn_begin</a>. If, however, the
24
parent argument is a <a href="../../api_c/txn_class.html">DB_TXN</a> handle, the newly created transaction
25
will be treated as a nested transaction within the parent. Transactions
26
may nest arbitrarily deeply. For the purposes of this discussion,
27
transactions created with a parent identifier will be called
28
<i>child transactions</i>.
29
<p>Once a transaction becomes a parent, as long as any of its child
30
transactions are unresolved (that is, they have neither committed nor
31
aborted), the parent may not issue any Berkeley DB calls except to begin more
32
child transactions, or to commit or abort. For example, it may not
33
issue any access method or cursor calls. After all of a parent's
34
children have committed or aborted, the parent may again request
35
operations on its own behalf.
36
<p>The semantics of nested transactions are as follows. When a child
37
transaction is begun, it inherits all the locks of its parent. This
38
means that the child will never block waiting on a lock held by its
39
parent. Further, locks held by two children of the same parent will
40
also conflict. To make this concrete, consider the following set of
41
transactions and lock acquisitions.
42
<p>Transaction T1 is the parent transaction. It acquires a write lock on
43
item A and then begins two child transactions: C1 and C2. C1 also
44
wishes to acquire a write lock on A; this succeeds. If C2 attempts to
45
acquire a write lock on A, it will block until C1 releases the lock, at
46
which point it will succeed. Now, let's say that C1 acquires a write
47
lock on B. If C2 now attempts to obtain a lock on B, it will block.
48
However, let's now assume that C1 commits. Its locks are
49
anti-inherited, which means they are given to T1, so T1 will now hold
50
a lock on B. At this point, C2 would be unblocked and would then
52
<p>Child transactions are entirely subservient to their parent transaction.
53
They may abort, undoing their operations regardless of the eventual fate
54
of the parent. However, even if a child transaction commits, if its
55
parent transaction is eventually aborted, the child's changes are undone
56
and the child's transaction is effectively aborted. Any child
57
transactions that are not yet resolved when the parent commits or aborts
58
are resolved based on the parent's resolution -- committing if the
59
parent commits and aborting if the parent aborts. Any child
60
transactions that are not yet resolved when the parent prepares are also
62
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/cursor.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/admin.html"><img src="../../images/next.gif" alt="Next"></a>
64
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>