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>Checkpoints</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="Berkeley DB Programmer's Reference Guide" /> 10 <link rel="up" href="transapp.html" title="Chapter��11.�� Berkeley DB Transactional Data Store Applications" /> 11 <link rel="prev" href="transapp_deadlock.html" title="Deadlock detection" /> 12 <link rel="next" href="transapp_archival.html" title="Database and log file archival" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Checkpoints</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="transapp_deadlock.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��11.�� 23 Berkeley DB Transactional Data Store Applications 24 </th> 25 <td width="20%" align="right">��<a accesskey="n" href="transapp_archival.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="transapp_checkpoint"></a>Checkpoints</h2> 35 </div> 36 </div> 37 </div> 38 <p>The second component of the infrastructure is performing checkpoints of 39the log files. Performing checkpoints is necessary for two reasons.</p> 40 <p>First, you may be able to remove Berkeley DB log files from your database 41environment after a checkpoint. Change records are written into the log 42files when databases are modified, but the actual changes to the 43database are not necessarily written to disk. When a checkpoint is 44performed, changes to the database are written into the backing database 45file. Once the database pages are written, log files can be archived 46and removed from the database environment because they will never be 47needed for anything other than catastrophic failure. (Log files which 48are involved in active transactions may not be removed, and there must 49always be at least one log file in the database environment.)</p> 50 <p>The second reason to perform checkpoints is because checkpoint frequency 51is inversely proportional to the amount of time it takes to run database 52recovery after a system or application failure. This is because 53recovery after failure has to redo or undo changes only since the last 54checkpoint, as changes before the checkpoint have all been flushed to 55the databases.</p> 56 <p> 57 Berkeley DB provides the <a href="../api_reference/C/db_checkpoint.html" class="olink">db_checkpoint utility</a>, which can be used to perform 58 checkpoints. Alternatively, applications can write their own 59 checkpoint thread using the underlying <a href="../api_reference/C/txncheckpoint.html" class="olink">DB_ENV->txn_checkpoint()</a> function. The 60 following code fragment checkpoints the database environment every 60 61 seconds: 62</p> 63 <pre class="programlisting">int 64main(int argc, char *argv) 65{ 66 extern int optind; 67 DB *db_cats, *db_color, *db_fruit; 68 DB_ENV *dbenv; 69 pthread_t ptid; 70 int ch; 71 72 while ((ch = getopt(argc, argv, "")) != EOF) 73 switch (ch) { 74 case '?': 75 default: 76 usage(); 77 } 78 argc -= optind; 79 argv += optind; 80 81 env_dir_create(); 82 env_open(&dbenv); 83 84<span class="bold"><strong> /* Start a checkpoint thread. */ 85 if ((errno = pthread_create( 86 &ptid, NULL, checkpoint_thread, (void *)dbenv)) != 0) { 87 fprintf(stderr, 88 "txnapp: failed spawning checkpoint thread: %s\n", 89 strerror(errno)); 90 exit (1); 91 }</strong></span> 92 93 /* Open database: Key is fruit class; Data is specific type. */ 94 db_open(dbenv, &db_fruit, "fruit", 0); 95 96 /* Open database: Key is a color; Data is an integer. */ 97 db_open(dbenv, &db_color, "color", 0); 98 99 /* 100 * Open database: 101 * Key is a name; Data is: company name, cat breeds. 102 */ 103 db_open(dbenv, &db_cats, "cats", 1); 104 105 add_fruit(dbenv, db_fruit, "apple", "yellow delicious"); 106 107 add_color(dbenv, db_color, "blue", 0); 108 add_color(dbenv, db_color, "blue", 3); 109 110 add_cat(dbenv, db_cats, 111 "Amy Adams", 112 "Oracle", 113 "abyssinian", 114 "bengal", 115 "chartreaux", 116 NULL); 117 118 return (0); 119} 120 121<span class="bold"><strong>void * 122checkpoint_thread(void *arg) 123{ 124 DB_ENV *dbenv; 125 int ret; 126 127 dbenv = arg; 128 dbenv->errx(dbenv, "Checkpoint thread: %lu", (u_long)pthread_self()); 129 130 /* Checkpoint once a minute. */ 131 for (;; sleep(60)) 132 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0)) != 0) { 133 dbenv->err(dbenv, ret, "checkpoint thread"); 134 exit (1); 135 } 136 137 /* NOTREACHED */ 138}</strong></span></pre> 139 <p>Because checkpoints can be quite expensive, choosing how often to 140perform a checkpoint is a common tuning parameter for Berkeley DB 141applications.</p> 142 </div> 143 <div class="navfooter"> 144 <hr /> 145 <table width="100%" summary="Navigation footer"> 146 <tr> 147 <td width="40%" align="left"><a accesskey="p" href="transapp_deadlock.html">Prev</a>��</td> 148 <td width="20%" align="center"> 149 <a accesskey="u" href="transapp.html">Up</a> 150 </td> 151 <td width="40%" align="right">��<a accesskey="n" href="transapp_archival.html">Next</a></td> 152 </tr> 153 <tr> 154 <td width="40%" align="left" valign="top">Deadlock detection��</td> 155 <td width="20%" align="center"> 156 <a accesskey="h" href="index.html">Home</a> 157 </td> 158 <td width="40%" align="right" valign="top">��Database and log file archival</td> 159 </tr> 160 </table> 161 </div> 162 </body> 163</html> 164