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>Configuring the Transaction Subsystem</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="usingtxns.html" title="Chapter��3.��Transaction Basics" /> 11 <link rel="prev" href="txnindices.html" title="Secondary Indices with Transaction Applications" /> 12 <link rel="next" href="txnconcurrency.html" title="Chapter��4.��Concurrency" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Configuring the Transaction Subsystem</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="txnindices.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��3.��Transaction Basics</th> 23 <td width="20%" align="right">��<a accesskey="n" href="txnconcurrency.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="maxtxns"></a>Configuring the Transaction Subsystem</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Most of the configuration activities that you need to perform 38 for your transactional DB application will involve the 39 locking and logging subsystems. See 40 <a class="xref" href="txnconcurrency.html" title="Chapter��4.��Concurrency">Concurrency</a> 41 and 42 <a class="xref" href="filemanagement.html" title="Chapter��5.��Managing DB Files">Managing DB Files</a> 43 for details. 44 </p> 45 <p> 46 However, there are a couple of things that you can do to 47 configure your transaction subsystem directly. These things 48 are: 49 </p> 50 <div class="itemizedlist"> 51 <ul type="disc"> 52 <li> 53 <span> 54 <p> 55 56 <span> 57 Configure 58 </span> 59 60 the maximum number of simultaneous 61 transactions needed by your application. 62 In general, you should not need to do this unless you 63 use deeply nested transactions or you have many threads all 64 of which have active transactions. In addition, you may 65 need to configure a higher maximum number of transactions if you 66 are using snapshot isolation. See 67 <a class="xref" href="isolation.html#sisolation_maxtxn" title="Snapshot Isolation Transactional Requirements">Snapshot Isolation Transactional Requirements</a> 68 for details. 69 </p> 70 <p> 71 By default, your application can support 20 active 72 transactions. 73 </p> 74 <p> 75 You can set the maximum number of simultaneous 76 transactions supported by your application using 77 <span> 78 the 79 <code class="methodname">DB_ENV->set_tx_max()</code> 80 81 method. Note that this method must be called 82 before the environment has been opened. 83 </span> 84 85 </p> 86 <p> 87 If your application has exceeded this maximum value, 88 then any attempt to begin a new transaction will fail. 89 </p> 90 <p> 91 This value can also be set using the 92 <code class="literal">DB_CONFIG</code> file's 93 <code class="literal">set_tx_max</code> parameter. Remember that 94 the <code class="literal">DB_CONFIG</code> must reside in your 95 environment home directory. 96 </p> 97</span> 98 </li> 99 <li> 100 <span> 101 <p> 102 <span> 103 Configure the timeout value for your transactions. 104 </span> 105 106 107 This value represents the longest period of time a 108 transaction can be active. Note, however, that 109 transaction timeouts are checked only when DB 110 examines its lock tables for blocked locks 111 (see <a class="xref" href="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks">Locks, Blocks, and Deadlocks</a> 112 for more information). Therefore, a transaction's timeout can 113 have expired, but the application will not be notified until DB 114 has a reason to examine its lock tables. 115 </p> 116 <p> 117 Be aware that some transactions may be 118 inappropriately timed out before the transaction has a 119 chance to complete. You should therefore use this 120 mechanism only if you know your application 121 might have unacceptably long transactions and 122 you want to make sure your application will 123 not stall during their execution. 124 (This might happen if, for example, your 125 transaction blocks or requests too much 126 data.) 127 </p> 128 <p> 129 Note that by default transaction timeouts are set to 0 seconds, which means that they never time 130 out. 131 </p> 132 <p> 133 To set the maximum timeout value for your transactions, 134 use the 135 <span><code class="methodname">DB_ENV->set_timeout()</code></span> 136 137 138 method. This method configures the entire 139 environment; not just the handle used to set the 140 configuration. Further, this value may 141 be set at any time during the application's 142 lifetime. 143 </p> 144 <p> 145 This value can also be set using the 146 <code class="literal">DB_CONFIG</code> file's 147 <code class="literal">set_txn_timeout</code> parameter. 148 </p> 149 150</span> 151 </li> 152 </ul> 153 </div> 154 <p> 155 For example: 156 </p> 157 <pre class="programlisting">#include <stdio.h> 158#include <stdlib.h> 159 160#include "db.h" 161 162int 163main(void) 164{ 165 int ret, ret_c; 166 u_int32_t db_flags, env_flags; 167 DB *dbp; 168 DB_ENV *envp; 169 DB_TXN *txn; 170 const char *db_home_dir = "/tmp/myEnvironment"; 171 const char *file_name = "mydb.db"; 172 173 envp = NULL; 174 175 /* Open the environment */ 176 ret = db_env_create(&envp, 0); 177 if (ret != 0) { 178 fprintf(stderr, "Error creating environment handle: %s\n", 179 db_strerror(ret)); 180 return (EXIT_FAILURE); 181 } 182 183 env_flags = DB_CREATE | /* If the environment does not 184 * exist, create it. */ 185 DB_INIT_LOCK | /* Initialize locking */ 186 DB_INIT_LOG | /* Initialize logging */ 187 DB_INIT_MPOOL | /* Initialize the cache */ 188 DB_THREAD | /* Free-thread the env handle. */ 189 DB_INIT_TXN; /* Initialize transactions */ 190 191 /* 192 * Configure a maximum transaction timeout of 1 second. 193 */ 194 ret = envp->set_timeout(envp, DB_SET_TXN_TIMEOUT, 1000000); 195 if (ret != 0) { 196 fprintf(stderr, "Error setting txn timeout: %s\n", 197 db_strerror(ret)); 198 goto err; 199 } 200 201 /* 202 * Configure 40 maximum transactions. 203 */ 204 ret = envp->set_tx_max(envp, 40); 205 if (ret != 0) { 206 fprintf(stderr, "Error setting max txns: %s\n", 207 db_strerror(ret)); 208 goto err; 209 } 210 211 ret = envp->open(envp, db_home_dir, env_flags, 0); 212 if (ret != 0) { 213 fprintf(stderr, "Error opening environment: %s\n", 214 db_strerror(ret)); 215 goto err; 216 } 217 218 /* 219 * From here, you open your databases, proceed with your 220 * database operations, and respond to deadlocks as 221 * is normal (omitted for brevity). 222 */ 223 ... </pre> 224 </div> 225 <div class="navfooter"> 226 <hr /> 227 <table width="100%" summary="Navigation footer"> 228 <tr> 229 <td width="40%" align="left"><a accesskey="p" href="txnindices.html">Prev</a>��</td> 230 <td width="20%" align="center"> 231 <a accesskey="u" href="usingtxns.html">Up</a> 232 </td> 233 <td width="40%" align="right">��<a accesskey="n" href="txnconcurrency.html">Next</a></td> 234 </tr> 235 <tr> 236 <td width="40%" align="left" valign="top">Secondary Indices with Transaction Applications��</td> 237 <td width="20%" align="center"> 238 <a accesskey="h" href="index.html">Home</a> 239 </td> 240 <td width="40%" align="right" valign="top">��Chapter��4.��Concurrency</td> 241 </tr> 242 </table> 243 </div> 244 </body> 245</html> 246