1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Opening a Transactional Environment and Database</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
<link rel="up" href="enabletxn.html" title="Chapter 2. Enabling Transactions" />
<link rel="prev" href="enabletxn.html" title="Chapter 2. Enabling Transactions" />
<link rel="next" href="usingtxns.html" title="Chapter 3. Transaction Basics" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Opening a Transactional Environment and
<span>Database</span>
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a> </td>
<th width="60%" align="center">Chapter 2. Enabling Transactions</th>
<td width="20%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="envopen"></a>Opening a Transactional Environment and
<span>Database</span>
</h2>
</div>
</div>
</div>
<p>
To enable transactions for your environment, you must initialize the
transactional subsystem. Note that doing this also initializes the
logging subsystem. In addition, you must initialize the memory pool
(in-memory cache). You must also initialize the locking subsystem.
<span>
For example:
</span>
</p>
<p>
Notice in the following example that you create your environment
handle using the <code class="function">db_env_create()</code> function before you open
the environment:
</p>
<pre class="programlisting">#include <stdio.h>
#include <stdlib.h>
#include "db.h"
int
main(void)
{
int ret, ret_c;
u_int32_t env_flags;
DB_ENV *envp;
const char *db_home_dir = "/tmp/myEnvironment";
envp = NULL;
/* Open the environment */
ret = db_env_create(&envp, 0);
if (ret != 0) {
fprintf(stderr, "Error creating environment handle: %s\n",
db_strerror(ret));
return (EXIT_FAILURE);
}
env_flags = DB_CREATE | /* Create the environment if it does
* not already exist. */
DB_INIT_TXN | /* Initialize transactions */
DB_INIT_LOCK | /* Initialize locking. */
DB_INIT_LOG | /* Initialize logging */
DB_INIT_MPOOL; /* Initialize the in-memory cache. */
ret = envp->open(envp, db_home_dir, env_flags, 0);
if (ret != 0) {
fprintf(stderr, "Error opening environment: %s\n",
db_strerror(ret));
goto err;
}
err:
/* Close the environment */
if (envp != NULL) {
ret_c = envp->close(envp, 0);
if (ret_c != 0) {
fprintf(stderr, "environment close failed: %s\n",
db_strerror(ret_c));
ret = ret_c;
}
}
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
} </pre>
<p>
You then create and open your database(s) as you would for a non-transactional system.
<span>
The only difference is that you must pass the environment handle to
the
<span>
<code class="function">db_create()</code> function,
</span>
and you must open the database within a transaction.
Typically auto commit is used for this purpose. To do so, pass
<code class="literal">DB_AUTO_COMMIT</code> to the database open command.
It is recommended that you close all your databases before you close
your environment.
For example:
</span>
</p>
<pre class="programlisting">#include <stdio.h>
#include <stdlib.h>
#include "db.h"
int
main(void)
{
int ret, ret_c;
u_int32_t <strong class="userinput"><code>db_flags,</code></strong> env_flags;
<strong class="userinput"><code>DB *dbp;</code></strong>
DB_ENV *envp;
const char *db_home_dir = "/tmp/myEnvironment";
<strong class="userinput"><code>const char *file_name = "mydb.db";
dbp = NULL;</code></strong>
envp = NULL;
/* Open the environment */
ret = db_env_create(&envp, 0);
if (ret != 0) {
fprintf(stderr, "Error creating environment handle: %s\n",
db_strerror(ret));
return (EXIT_FAILURE);
}
env_flags = DB_CREATE | /* Create the environment if it does
* not already exist. */
DB_INIT_TXN | /* Initialize transactions */
DB_INIT_LOCK | /* Initialize locking. */
DB_INIT_LOG | /* Initialize logging */
DB_INIT_MPOOL; /* Initialize the in-memory cache. */
ret = envp->open(envp, db_home_dir, env_flags, 0);
if (ret != 0) {
fprintf(stderr, "Error opening environment: %s\n",
db_strerror(ret));
goto err;
}
<strong class="userinput"><code>/* Initialize the DB handle */
ret = db_create(&dbp, envp, 0);
if (ret != 0) {
envp->err(envp, ret, "Database creation failed");
goto err;
}
db_flags = DB_CREATE | DB_AUTO_COMMIT;
ret = dbp->open(dbp, /* Pointer to the database */
NULL, /* Txn pointer */
file_name, /* File name */
NULL, /* Logical db name */
DB_BTREE, /* Database type (using btree) */
db_flags, /* Open flags */
0); /* File mode. Using defaults */
if (ret != 0) {
envp->err(envp, ret, "Database '%s' open failed",
file_name);
goto err;
}</code></strong>
err:
<strong class="userinput"><code>/* Close the database */
if (dbp != NULL) {
ret_c = dbp->close(dbp, 0);
if (ret_c != 0) {
envp->err(envp, ret_c, "Database close failed.");
ret = ret_c;
}
}</code></strong>
/* Close the environment */
if (envp != NULL) {
ret_c = envp->close(envp, 0);
if (ret_c != 0) {
fprintf(stderr, "environment close failed: %s\n",
db_strerror(ret_c));
ret = ret_c;
}
}
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
} </pre>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
Never close a database that has active transactions. Make sure
all transactions are resolved (either committed or aborted)
before closing the database.
</p>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="enabletxn.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 2. Enabling Transactions </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 3. Transaction Basics</td>
</tr>
</table>
</div>
</body>
</html>
|