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