data_open.html   [plain text]


<!--$Id: data_open.so,v 1.13 2004/04/28 13:15:29 bostic Exp $-->
<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Opening the databases</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<table width="100%"><tr valign=top>
<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></b></td>
<td align=right><a href="../transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p align=center><b>Opening the databases</b></p>
<p>Next, we open three databases ("color" and "fruit" and "cats"), in the
database environment.  Again, our <a href="../../api_c/db_class.html">DB</a> database handles are
declared to be free-threaded using the <a href="../../api_c/env_open.html#DB_THREAD">DB_THREAD</a> flag, and so
may be used by any number of threads we subsequently create.</p>
<blockquote><pre>int
main(int argc, char *argv)
{
	extern int optind;
	DB *db_cats, *db_color, *db_fruit;
	DB_ENV *dbenv;
	int ch;
<p>
	while ((ch = getopt(argc, argv, "")) != EOF)
		switch (ch) {
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;
<p>
	env_dir_create();
	env_open(&dbenv);
<p>
<b>	/* Open database: Key is fruit class; Data is specific type. */
	if (db_open(dbenv, &db_fruit, "fruit", 0))
		return (1);
<p>
	/* Open database: Key is a color; Data is an integer. */
	if (db_open(dbenv, &db_color, "color", 0))
		return (1);
<p>
	/*
	 * Open database:
	 *	Key is a name; Data is: company name, cat breeds.
	 */
	if (db_open(dbenv, &db_cats, "cats", 1))
		return (1);</b>
<p>
	return (0);
}
<p>
<b>int
db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
{
	DB *db;
	int ret;
<p>
	/* Create the database handle. */
	if ((ret = db_create(&db, dbenv, 0)) != 0) {
		dbenv-&gt;err(dbenv, ret, "db_create");
		return (1);
	}
<p>
	/* Optionally, turn on duplicate data items. */
	if (dups && (ret = db-&gt;set_flags(db, DB_DUP)) != 0) {
		(void)db-&gt;close(db, 0);
		dbenv-&gt;err(dbenv, ret, "db-&gt;set_flags: DB_DUP");
		return (1);
	}
<p>
	/*
	 * Open a database in the environment:
	 *	create if it doesn't exist
	 *	free-threaded handle
	 *	read/write owner only
	 */
	if ((ret = db-&gt;open(db, NULL, name, NULL, DB_BTREE,
	    DB_AUTO_COMMIT | DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) {
		(void)db-&gt;close(db, 0);
		dbenv-&gt;err(dbenv, ret, "db-&gt;open: %s", name);
		return (1);
	}
<p>
	*dbp = db;
	return (0);
}</b></pre></blockquote>
<p>After opening the database, we can use the <a href="../../utility/db_stat.html">db_stat</a> utility to
display information about a database we have created:</p>
<blockquote><pre>prompt&gt; db_stat -h TXNAPP -d color
53162   Btree magic number.
8       Btree version number.
Flags:
2       Minimum keys per-page.
8192    Underlying database page size.
1       Number of levels in the tree.
0       Number of unique keys in the tree.
0       Number of data items in the tree.
0       Number of tree internal pages.
0       Number of bytes free in tree internal pages (0% ff).
1       Number of tree leaf pages.
8166    Number of bytes free in tree leaf pages (0.% ff).
0       Number of tree duplicate pages.
0       Number of bytes free in tree duplicate pages (0% ff).
0       Number of tree overflow pages.
0       Number of bytes free in tree overflow pages (0% ff).
0       Number of pages on the free list.</pre></blockquote>
<p>The database open must be enclosed within a transaction in order to be
recoverable.  The transaction will ensure that created files are
re-created in recovered environments (or do not appear at all).
Additional database operations or operations on other databases can be
included in the same transaction, of course.  In the simple case, where
the open is the only operation in the transaction, an application can
set the <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag instead of creating and managing
its own transaction handle.  The <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag will
internally wrap the operation in a transaction, simplifying application
code.</p>
<p>The previous example is the simplest case of transaction protection for
database open.  Obviously, additional database operations can be done
in the scope of the same transaction.  For example, an application
maintaining a list of the databases in a database environment in a
well-known file might include an update of the list in the same
transaction in which the database is created.  Or, an application might
create both a primary and secondary database in a single transaction.</p>
<p><a href="../../api_c/db_class.html">DB</a> handles that will later be used for transactionally protected
database operations must be opened within a transaction.  Specifying a
transaction handle to database operations using <a href="../../api_c/db_class.html">DB</a> handles not
opened within a transaction will return an error.  Similarly, not
specifying a transaction handle to database operations that will modify
the database, using handles that were opened within a transaction, will
also return an error.</p>
<table width="100%"><tr><td><br></td><td align=right><a href="../transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
</body>
</html>