• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500-V1.0.1.40_1.0.68/ap/gpl/timemachine/db-4.7.25.NC/docs/gsg_txn/CXX/
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>Opening a Transactional Environment and
7            Database
8            
9            
10        </title>
11    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
12    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
13    <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
14    <link rel="up" href="enabletxn.html" title="Chapter��2.��Enabling Transactions" />
15    <link rel="previous" href="enabletxn.html" title="Chapter��2.��Enabling Transactions" />
16    <link rel="next" href="usingtxns.html" title="Chapter��3.��Transaction Basics" />
17  </head>
18  <body>
19    <div class="navheader">
20      <table width="100%" summary="Navigation header">
21        <tr>
22          <th colspan="3" align="center">Opening a Transactional Environment and
23            Database
24            
25            
26        </th>
27        </tr>
28        <tr>
29          <td width="20%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td>
30          <th width="60%" align="center">Chapter��2.��Enabling Transactions</th>
31          <td width="20%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td>
32        </tr>
33      </table>
34      <hr />
35    </div>
36    <div class="sect1" lang="en" xml:lang="en">
37      <div class="titlepage">
38        <div>
39          <div>
40            <h2 class="title" style="clear: both"><a id="envopen"></a>Opening a Transactional Environment and
41            <span>Database</span>
42            
43            
44        </h2>
45          </div>
46        </div>
47        <div></div>
48      </div>
49      <p>
50            To enable transactions for your environment, you must initialize the
51            transactional subsystem. Note that doing this also initializes the
52            logging subsystem. In addition, you must initialize the memory pool
53            (in-memory cache). Frequently, but not always, you will also 
54            initialize the locking subsystem.  
55            
56            
57            <span>
58                For example:
59            </span>
60            
61        </p>
62      <pre class="programlisting">#include "db_cxx.h"
63
64...
65                                                                                                                                  
66int main(void)
67{
68    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
69                                           // exist, create it.
70                          DB_INIT_LOCK  |  // Initialize locking
71                          DB_INIT_LOG   |  // Initialize logging
72                          DB_INIT_MPOOL |  // Initialize the cache
73                          DB_INIT_TXN;     // Initialize transactions
74                                                                                                                                  
75    std::string envHome("/export1/testEnv");
76    DbEnv myEnv(0);
77
78    try {
79
80        myEnv.open(envHome.c_str(), env_flags, 0);
81
82    } catch(DbException &amp;e) {
83        std::cerr &lt;&lt; "Error opening database environment: "
84                  &lt;&lt; envHome &lt;&lt; std::endl;
85        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
86        return (EXIT_FAILURE);
87    }
88
89    try {
90        myEnv.close(0);
91    } catch(DbException &amp;e) {
92        std::cerr &lt;&lt; "Error closing database environment: "
93                &lt;&lt; envHome &lt;&lt; std::endl;
94        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
95        return (EXIT_FAILURE);
96    }
97
98    return (EXIT_SUCCESS);
99} </pre>
100      <p>
101        You then create and open your database(s) as you would for a non-transactional system.
102        <span>
103            The only difference is that you must pass the environment handle to
104            the 
105                
106                <span>
107                    <tt class="methodname">DbEnv::open()</tt> method,
108                </span>
109             and you must open the database within a transaction.
110             Typically auto commit is used for this purpose. To do so, pass 
111            <tt class="literal">DB_AUTO_COMMIT</tt> to the database open command.
112            Also, make sure you close all your databases before you close 
113            your environment.
114            For example:
115        </span>
116
117        
118    </p>
119      <pre class="programlisting">#include "db_cxx.h"
120
121...
122                                                                                                                                  
123int main(void)
124{
125    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
126                                           // exist, create it.
127                          DB_INIT_LOCK  |  // Initialize locking
128                          DB_INIT_LOG   |  // Initialize logging
129                          DB_INIT_MPOOL |  // Initialize the cache
130                          DB_INIT_TXN;     // Initialize transactions
131
132    <b class="userinput"><tt>u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT;
133    Db *dbp = NULL;
134    const char *file_name = "mydb.db";</tt></b>
135                                                                                                                                  
136    std::string envHome("/export1/testEnv");
137    DbEnv myEnv(0);
138
139    try {
140
141        myEnv.open(envHome.c_str(), env_flags, 0);
142        <b class="userinput"><tt>dbp = new Db(&amp;myEnv, 0);
143        dbp-&gt;open(NULL,       // Txn pointer 
144                  file_name,  // File name 
145                  NULL,       // Logical db name 
146                  DB_BTREE,   // Database type (using btree) 
147                  db_flags,   // Open flags 
148                  0);         // File mode. Using defaults </tt></b>
149
150    } catch(DbException &amp;e) {
151        std::cerr &lt;&lt; "Error opening database and environment: "
152                  &lt;&lt; file_name &lt;&lt; ", "
153                  &lt;&lt; envHome &lt;&lt; std::endl;
154        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
155    }
156
157    try {
158        <b class="userinput"><tt>dbp-&gt;close(0);</tt></b>
159        myEnv.close(0);
160    } catch(DbException &amp;e) {
161        std::cerr &lt;&lt; "Error closing database and environment: "
162                  &lt;&lt; file_name &lt;&lt; ", "
163                  &lt;&lt; envHome &lt;&lt; std::endl;
164        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
165        return (EXIT_FAILURE);
166    }
167
168    return (EXIT_SUCCESS);
169} </pre>
170      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
171        <h3 class="title">Note</h3>
172        <p>
173                Never close a database  that has active transactions. Make sure
174            all transactions are resolved (either committed or aborted)
175            before closing the database.
176        </p>
177      </div>
178    </div>
179    <div class="navfooter">
180      <hr />
181      <table width="100%" summary="Navigation footer">
182        <tr>
183          <td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td>
184          <td width="20%" align="center">
185            <a accesskey="u" href="enabletxn.html">Up</a>
186          </td>
187          <td width="40%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td>
188        </tr>
189        <tr>
190          <td width="40%" align="left" valign="top">Chapter��2.��Enabling Transactions��</td>
191          <td width="20%" align="center">
192            <a accesskey="h" href="index.html">Home</a>
193          </td>
194          <td width="40%" align="right" valign="top">��Chapter��3.��Transaction Basics</td>
195        </tr>
196      </table>
197    </div>
198  </body>
199</html>
200