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��6.��Summary and Examples</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="Getting Started with Berkeley DB Transaction Processing" />
10    <link rel="up" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
11    <link rel="prev" href="logconfig.html" title="Configuring the Logging Subsystem" />
12    <link rel="next" href="txnexample_c.html" title="Transaction Example" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��6.��Summary and Examples</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="logconfig.html">Prev</a>��</td>
22          <th width="60%" align="center">��</th>
23          <td width="20%" align="right">��<a accesskey="n" href="txnexample_c.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="wrapup"></a>Chapter��6.��Summary and Examples</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="wrapup.html#anatomy">Anatomy of a Transactional Application</a>
44            </span>
45          </dt>
46          <dt>
47            <span class="sect1">
48              <a href="txnexample_c.html">Transaction Example</a>
49            </span>
50          </dt>
51          <dt>
52            <span class="sect1">
53              <a href="inmem_txnexample_c.html">In-Memory Transaction Example</a>
54            </span>
55          </dt>
56        </dl>
57      </div>
58      <p>
59        Throughout this manual we have presented the concepts and
60        mechanisms that you need to provide transactional protection for
61        your application. In this chapter, we summarize these
62        mechanisms, and we provide a complete example of a multi-threaded
63        transactional DB application.
64  </p>
65      <div class="sect1" lang="en" xml:lang="en">
66        <div class="titlepage">
67          <div>
68            <div>
69              <h2 class="title" style="clear: both"><a id="anatomy"></a>Anatomy of a Transactional Application</h2>
70            </div>
71          </div>
72        </div>
73        <p>
74        Transactional applications are characterized by performing the
75        following activities:
76    </p>
77        <div class="orderedlist">
78          <ol type="1">
79            <li>
80              <p>
81                Create your environment handle.
82            </p>
83            </li>
84            <li>
85              <p>
86                Open your environment, specifying that the following
87                subsystems be used:
88            </p>
89              <div class="itemizedlist">
90                <ul type="disc">
91                  <li>
92                    <p>
93                        Transactional Subsystem (this also initializes the
94                        logging subsystem).
95                    </p>
96                  </li>
97                  <li>
98                    <p>
99                        Memory pool (the in-memory cache).
100                    </p>
101                  </li>
102                  <li>
103                    <p>
104                        Logging subsystem.
105                    </p>
106                  </li>
107                  <li>
108                    <p>
109                        Locking subsystem (if your application is multi-process or multi-threaded).
110                    </p>
111                  </li>
112                </ul>
113              </div>
114              <p>
115                It is also highly recommended that you run normal recovery 
116                upon first environment open. Normal recovery examines only those logs required
117                to ensure your database files are consistent relative to the information found in your
118                log files.
119            </p>
120            </li>
121            <li>
122              <p>
123                Optionally spawn off any utility threads that you might need. Utility
124                threads can be used to run checkpoints periodically, or to
125                periodically run a deadlock detector if you do not want to
126                use DB's built-in deadlock detector.
127            </p>
128            </li>
129            <li>
130              <p>
131                Open whatever database handles that you need.
132            </p>
133            </li>
134            <li>
135              <p>
136                Spawn off worker threads. How many of these you need and
137                how they split their DB workload is entirely up to your
138                application's requirements. However, any worker threads
139                that perform write operations will do the following:
140            </p>
141              <div class="orderedlist">
142                <ol type="a">
143                  <li>
144                    <p>
145                        Begin a transaction.
146                    </p>
147                  </li>
148                  <li>
149                    <p>
150                        Perform one or more read and write
151                        operations.
152                    </p>
153                  </li>
154                  <li>
155                    <p>
156                        Commit the transaction if all goes well.
157                    </p>
158                  </li>
159                  <li>
160                    <p>
161                        Abort and retry the operation if a deadlock is
162                        detected.
163                    </p>
164                  </li>
165                  <li>
166                    <p>
167                        Abort the transaction for most other errors.
168                    </p>
169                  </li>
170                </ol>
171              </div>
172            </li>
173            <li>
174              <p>
175                On application shutdown:
176            </p>
177              <div class="orderedlist">
178                <ol type="a">
179                  <li>
180                    <p>
181                        Make sure there are no opened cursors.
182                    </p>
183                  </li>
184                  <li>
185                    <p>
186                        Make sure there are no active transactions. Either
187                        abort or commit all transactions before shutting
188                        down.
189                    </p>
190                  </li>
191                  <li>
192                    <p>
193                            Close your databases.
194                    </p>
195                  </li>
196                  <li>
197                    <p>
198                        Close your environment.
199                    </p>
200                  </li>
201                </ol>
202              </div>
203            </li>
204          </ol>
205        </div>
206        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
207          <h3 class="title">Note</h3>
208          <p>
209                Robust DB applications should monitor their worker threads to
210            make sure they have not died unexpectedly. If a thread does
211            terminate abnormally, you must shutdown all your worker threads
212            and then run normal recovery (you will have to reopen your
213            environment to do this). This is the only way to clear any
214            resources (such as a lock or a mutex) that the abnormally
215            exiting worker thread might have been holding at the time that
216            it died.
217        </p>
218          <p>
219            Failure to perform this recovery can cause your
220            still-functioning worker threads to eventually block forever
221            while waiting for a lock that will never be released.
222        </p>
223        </div>
224        <p>
225        In addition to these activities, which are all entirely handled by
226        code within your application, there are some administrative
227        activities that you should perform:
228    </p>
229        <div class="itemizedlist">
230          <ul type="disc">
231            <li>
232              <p>
233                Periodically checkpoint your application. Checkpoints will
234                reduce the time to run recovery in the event that one is
235                required. See <a class="xref" href="filemanagement.html#checkpoints" title="Checkpoints">Checkpoints</a>
236                for details.
237            </p>
238            </li>
239            <li>
240              <p>
241                Periodically back up your database and log files. This is
242                required in order to fully obtain the durability guarantee
243                made by DB's transaction ACID support. See
244                <a class="xref" href="backuprestore.html" title="Backup Procedures">Backup Procedures</a>
245                for more information.
246            </p>
247            </li>
248            <li>
249              <p>
250                You may want to maintain a hot failover if 24x7 processing
251                with rapid restart in the face of a disk hit is important
252                to you. See <a class="xref" href="hotfailover.html" title="Using Hot Failovers">Using Hot Failovers</a>
253                for more information.
254            </p>
255            </li>
256          </ul>
257        </div>
258      </div>
259    </div>
260    <div class="navfooter">
261      <hr />
262      <table width="100%" summary="Navigation footer">
263        <tr>
264          <td width="40%" align="left"><a accesskey="p" href="logconfig.html">Prev</a>��</td>
265          <td width="20%" align="center">��</td>
266          <td width="40%" align="right">��<a accesskey="n" href="txnexample_c.html">Next</a></td>
267        </tr>
268        <tr>
269          <td width="40%" align="left" valign="top">Configuring the Logging Subsystem��</td>
270          <td width="20%" align="center">
271            <a accesskey="h" href="index.html">Home</a>
272          </td>
273          <td width="40%" align="right" valign="top">��Transaction Example</td>
274        </tr>
275      </table>
276    </div>
277  </body>
278</html>
279