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