2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Opening the databases</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
14
<td align=right><a href="../../ref/transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Opening the databases</h1>
18
<p>Next, we open three databases ("color" and "fruit" and "cats"), in the
19
database environment. Again, our <a href="../../api_c/db_class.html">DB</a> database handles are
20
declared to be free-threaded using the <a href="../../api_c/env_open.html#DB_THREAD">DB_THREAD</a> flag, and so
21
may be used by any number of threads we subsequently create.
22
<p><blockquote><pre>int
23
main(int argc, char *argv)
27
DB *db_cats, *db_color, *db_fruit;
31
while ((ch = getopt(argc, argv, "")) != EOF)
43
<b> /* Open database: Key is fruit class; Data is specific type. */
44
if (db_open(dbenv, &db_fruit, "fruit", 0))
47
/* Open database: Key is a color; Data is an integer. */
48
if (db_open(dbenv, &db_color, "color", 0))
53
* Key is a name; Data is: company name, cat breeds.
55
if (db_open(dbenv, &db_cats, "cats", 1))
62
db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
67
/* Create the database handle. */
68
if ((ret = db_create(&db, dbenv, 0)) != 0) {
69
dbenv->err(dbenv, ret, "db_create");
73
/* Optionally, turn on duplicate data items. */
74
if (dups && (ret = db->set_flags(db, DB_DUP)) != 0) {
75
(void)db->close(db, 0);
76
dbenv->err(dbenv, ret, "db->set_flags: DB_DUP");
81
* Open a database in the environment:
82
* create if it doesn't exist
83
* free-threaded handle
84
* read/write owner only
86
if ((ret = db->open(db, NULL, name, NULL, DB_BTREE,
87
DB_CREATE | DB_THREAD | DB_AUTO_COMMIT, S_IRUSR | S_IWUSR)) != 0) {
88
(void)db->close(db, 0);
89
dbenv->err(dbenv, ret, "db->open: %s", name);
95
}</b></pre></blockquote>
96
<p>After opening the database, we can use the <a href="../../utility/db_stat.html">db_stat</a> utility to
97
display information about a database we have created:
98
<p><blockquote><pre>prompt> db_stat -h TXNAPP -d color
99
53162 Btree magic number.
100
8 Btree version number.
102
2 Minimum keys per-page.
103
8192 Underlying database page size.
104
1 Number of levels in the tree.
105
0 Number of unique keys in the tree.
106
0 Number of data items in the tree.
107
0 Number of tree internal pages.
108
0 Number of bytes free in tree internal pages (0% ff).
109
1 Number of tree leaf pages.
110
8166 Number of bytes free in tree leaf pages (0.% ff).
111
0 Number of tree duplicate pages.
112
0 Number of bytes free in tree duplicate pages (0% ff).
113
0 Number of tree overflow pages.
114
0 Number of bytes free in tree overflow pages (0% ff).
115
0 Number of pages on the free list.</pre></blockquote>
116
<p>The database open must be enclosed within a transaction in order to be
117
recoverable. The transaction will ensure that created files are
118
re-created in recovered environments (or do not appear at all).
119
Additional database operations or operations on other databases can be
120
included in the same transaction, of course. In the simple case, where
121
the open is the only operation in the transaction, an application can
122
set the <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag instead of creating and managing
123
its own transaction handle. The <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag will
124
internally wrap the operation in a transaction, simplifying application
126
<p>The previous example is the simplest case of transaction protection for
127
database open. Obviously, additional database operations can be done
128
in the scope of the same transaction. For example, an application
129
maintaining a list of the databases in a database environment in a
130
well-known file might include an update of the list in the same
131
transaction in which the database is created. Or, an application might
132
create both a primary and secondary database in a single transaction.
133
<p><a href="../../api_c/db_class.html">DB</a> handles that will later be used for transactionally protected
134
operations must be opened within a transaction. Specifying a
135
transaction handle to operations using handles not opened within a
136
transaction will return an error. Similarly, not specifying a
137
transaction handle to operations using handles that were opened within
138
a transaction will also return an error.
139
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
141
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>