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