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