1<!--$Id: db_open.so,v 10.124 2007/10/24 16:06:06 bostic Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB: Db::open</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td>
13<b>Db::open</b>
14</td>
15<td align=right>
16<a href="../api_cxx/api_core.html"><img src="../images/api.gif" alt="API"></a>
17<a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a></td>
18</tr></table>
19<hr size=1 noshade>
20<tt>
21<b><pre>
22#include &lt;db_cxx.h&gt;
23<p>
24int
25Db::open(DbTxn *txnid, const char *file,
26    const char *database, DBTYPE type, u_int32_t flags, int mode);
27<p>
28int
29Db::get_dbname(const char **filenamep, const char **dbnamep);
30<p>
31int
32Db::get_multiple()
33<p>
34int
35Db::get_open_flags(u_int32_t *flagsp);
36<p>
37int
38Db::get_transactional()
39</pre></b>
40<hr size=1 noshade>
41<b>Description: Db::open</b>
42<p>The Db::open method opens the database represented by the <b>file</b>
43and <b>database</b> parameters for both reading and writing.</p>
44<p>The currently supported Berkeley DB file formats (or <i>access
45methods</i>) are Btree, Hash, Queue, and Recno.  The Btree format is a
46representation of a sorted, balanced tree structure.  The Hash format
47is an extensible, dynamic hashing scheme.  The Queue format supports
48fast access to fixed-length records accessed sequentially or by logical
49record number.  The Recno format supports fixed- or variable-length
50records, accessed sequentially or by logical record number, and
51optionally backed by a flat text file.</p>
52<p>Storage and retrieval for the Berkeley DB access methods are based on key/data
53pairs; see <a href="../api_cxx/dbt_class.html">Dbt</a> for more information.</p>
54<p>Calling Db::open is a relatively expensive operation, and
55maintaining a set of open databases will normally be preferable to
56repeatedly opening and closing the database for each new query.</p>
57<p>The Db::open method
58either returns a non-zero error value
59or throws an exception that encapsulates a non-zero error value on
60failure, and returns 0 on success.
61If Db::open fails, the <a href="../api_cxx/db_close.html">Db::close</a> method must be called to
62discard the <a href="../api_cxx/db_class.html">Db</a> handle.
63</p>
64<b>Parameters</b> <br>
65 <b>database</b><ul compact><li>The <b>database</b> parameter is optional, and allows applications to
66have multiple databases in a single file.  Although no <b>database</b>
67parameter needs to be specified, it is an error to attempt to open a
68second database in a <b>file</b> that was not initially created using
69a <b>database</b> name.  Further, the <b>database</b> parameter is not
70supported by the Queue format.  Finally, when opening multiple databases
71in the same physical file, it is important to consider locking and
72memory cache issues; see <a href="../ref/am/opensub.html">Opening
73multiple databases in a single file</a> for more information.
74<p>In-memory databases never intended to be preserved on disk may be
75created by setting the <b>file</b> parameter to NULL.  If the
76<b>database</b> parameter is also NULL, the database is strictly
77temporary and cannot be opened by any other thread of control, thus the
78database can only be accessed by sharing the single database handle that
79created it, in circumstances where doing so is safe.  If the
80<b>database</b> parameter is not set to NULL, the database can be opened
81by other threads of control and will be replicated to client sites in
82any replication group.</p></ul>
83 <b>file</b><ul compact><li>The <b>file</b> parameter is used as the name of an underlying file that
84will be used to back the database; see <a href="../ref/env/naming.html">File naming</a> for more information.
85<p>In-memory databases never intended to be preserved on disk may be
86created by setting the <b>file</b> parameter to NULL.</p></ul>
87<p>When using a Unicode build on Windows (the default), the <b>file</b>
88argument will be interpreted as a UTF-8 string, which is equivalent to
89ASCII for Latin characters.</p>
90 <b>flags</b><ul compact><li>The <b>flags</b> parameter must be set to 0 or by bitwise inclusively <b>OR</b>'ing together one
91or more of the following values:
92<br>
93<b><a name="DB_AUTO_COMMIT">DB_AUTO_COMMIT</a></b><ul compact><li>Enclose the Db::open call within a transaction.  If the call
94succeeds, the open operation will be recoverable and all subsequent
95database modification operations based on this handle will be
96transactionally protected.  If the call fails, no database will have
97been created.</ul>
98<b><a name="DB_CREATE">DB_CREATE</a></b><ul compact><li>Create the database.  If the database does not already exist and the
99DB_CREATE flag is not specified, the Db::open will fail.</ul>
100<b><a name="DB_EXCL">DB_EXCL</a></b><ul compact><li>Return an error if the database already exists.  The DB_EXCL
101flag is only meaningful when specified with the DB_CREATE
102flag.</ul>
103<b><a name="DB_MULTIVERSION">DB_MULTIVERSION</a></b><ul compact><li>Open the database with support for <a href="../ref/transapp/read.html">multiversion concurrency control</a>.  This will cause updates to the
104database to follow a copy-on-write protocol, which is required to
105support snapshot isolation.  The DB_MULTIVERSION flag requires
106that the database be transactionally protected during its open and is
107not supported by the queue format.</ul>
108<b><a name="DB_NOMMAP">DB_NOMMAP</a></b><ul compact><li>Do not map this database into process memory (see the
109<a href="../api_cxx/env_set_mp_mmapsize.html">DbEnv::set_mp_mmapsize</a> method for further information).</ul>
110<b><a name="DB_RDONLY">DB_RDONLY</a></b><ul compact><li>Open the database for reading only.  Any attempt to modify items in the
111database will fail, regardless of the actual permissions of any
112underlying files.</ul>
113<b><a name="DB_READ_UNCOMMITTED">DB_READ_UNCOMMITTED</a></b><ul compact><li>Support transactional read operations with degree 1 isolation.  Read
114operations on the database may request the return of modified but not
115yet committed data.  This flag must be specified on all <a href="../api_cxx/db_class.html">Db</a>
116handles used to perform dirty reads or database updates, otherwise
117requests for dirty reads may not be honored and the read may block.</ul>
118<b><a name="DB_THREAD">DB_THREAD</a></b><ul compact><li>Cause the <a href="../api_cxx/db_class.html">Db</a> handle returned by Db::open to be
119<i>free-threaded</i>; that is, concurrently usable by multiple
120threads in the address space.</ul>
121<b><a name="DB_TRUNCATE">DB_TRUNCATE</a></b><ul compact><li>Physically truncate the underlying file, discarding all previous
122databases it might have held.  Underlying filesystem primitives are used
123to implement this flag.  For this reason, it is applicable only to the
124file and cannot be used to discard databases within a file.
125<p>The DB_TRUNCATE flag cannot be lock or transaction-protected,
126and it is an error to specify it in a locking or transaction-protected
127environment.</p></ul>
128<br></ul>
129 <b>mode</b><ul compact><li>On Windows systems, the mode parameter is ignored.
130<p>On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, files created by the database open
131are created with mode <b>mode</b> (as described in <b>chmod</b>(2))
132and modified by the process' umask value at the time of creation (see
133<b>umask</b>(2)).  Created files are owned by the process owner; the
134group ownership of created files is based on the system and directory
135defaults, and is not further specified by Berkeley DB.  System shared memory
136segments created by the database open are created with mode <b>mode</b>, unmodified
137by the process' umask value.  If <b>mode</b> is 0, the database open will use a
138default mode of readable and writable by both owner and group.</p></ul>
139 <b>txnid</b><ul compact><li>If the operation is part of an application-specified transaction, the
140<b>txnid</b> parameter is a transaction handle returned from
141<a href="../api_cxx/txn_begin.html">DbEnv::txn_begin</a>; if the operation is part of a Berkeley DB Concurrent Data Store group, the
142<b>txnid</b> parameter is a handle returned from
143<a href="../api_cxx/env_cdsgroup_begin.html">DbEnv::cdsgroup_begin</a>; otherwise NULL.
144If no transaction handle is
145specified, but the
146DB_AUTO_COMMIT flag is specified,
147the operation will be implicitly transaction protected.
148Note that transactionally protected operations on a <a href="../api_cxx/db_class.html">Db</a> handle
149requires the <a href="../api_cxx/db_class.html">Db</a> handle itself be transactionally protected
150during its open.  Also note that the transaction must be committed before
151the handle is closed; see <a href="../ref/program/scope.html">Berkeley DB
152handles</a> for more information.</ul>
153 <b>type</b><ul compact><li>The <b>type</b> parameter is of type DBTYPE, and must be set to one of
154<a name="DB_BTREE">DB_BTREE</a>, <a name="DB_HASH">DB_HASH</a>, <a name="DB_QUEUE">DB_QUEUE</a>,
155<a name="DB_RECNO">DB_RECNO</a>, or <a name="DB_UNKNOWN">DB_UNKNOWN</a>.  If <b>type</b> is
156DB_UNKNOWN, the database must already exist and Db::open will
157automatically determine its type.  The <a href="../api_cxx/db_get_type.html">Db::get_type</a> method may be used
158to determine the underlying type of databases opened using DB_UNKNOWN.</ul>
159<br>
160<br><b>Environment Variables</b>
161<p>If the database was opened within a database environment, the
162environment variable <b>DB_HOME</b> may be used as the path of the
163database environment home.</p>
164<p>Db::open is affected by any database directory specified using
165the <a href="../api_cxx/env_set_data_dir.html">DbEnv::set_data_dir</a> method, or by setting the "set_data_dir" string
166in the environment's <a href="../ref/env/db_config.html#DB_CONFIG">DB_CONFIG</a> file.</p>
167<br>
168<b>TMPDIR</b><ul compact><li>If the <b>file</b> and <b>dbenv</b> parameters to Db::open are
169NULL, the environment variable <b>TMPDIR</b> may be used as a
170directory in which to create temporary backing files</ul>
171<br>
172<br><b>Errors</b>
173<br>
174<b>ENOENT</b><ul compact><li>The file or directory does not exist.</ul>
175<br>
176<p>The Db::open method
177may fail and throw
178<a href="../api_cxx/except_class.html">DbException</a>,
179encapsulating one of the following non-zero errors, or return one of
180the following non-zero errors:</p>
181<br>
182<b>DB_OLD_VERSION</b><ul compact><li>The database cannot be opened without being first upgraded.</ul>
183<br>
184<br>
185<b>EEXIST</b><ul compact><li>DB_CREATE and DB_EXCL were specified and the database exists.</ul>
186<br>
187<br>
188<b>EINVAL</b><ul compact><li>If an unknown database type, page size, hash function, pad byte, byte
189order, or a flag value or parameter that is incompatible with the
190specified database was specified;
191the <a href="../api_cxx/env_open.html#DB_THREAD">DB_THREAD</a> flag was specified and fast mutexes are not
192available for this architecture;
193the <a href="../api_cxx/env_open.html#DB_THREAD">DB_THREAD</a> flag was specified to Db::open, but was
194not specified to the <a href="../api_cxx/env_open.html">DbEnv::open</a> call for the environment in
195which the <a href="../api_cxx/db_class.html">Db</a> handle was created;
196a backing flat text file was specified with either the <a href="../api_cxx/env_open.html#DB_THREAD">DB_THREAD</a>
197flag or the provided database environment supports transaction
198processing; or if an
199invalid flag value or parameter was specified.</ul>
200<br>
201<br>
202<b>ENOENT</b><ul compact><li>A nonexistent <b>re_source</b> file was specified.</ul>
203<br>
204<br>
205<b>DB_REP_HANDLE_DEAD</b><ul compact><li>The database handle has been invalidated because a replication election
206unrolled a committed transaction.</ul>
207<br>
208<br>
209<b>DB_REP_LOCKOUT</b><ul compact><li>The operation was blocked by client/master synchronization.</ul>
210<br>
211<p>If a transactional database environment operation was selected to
212resolve a deadlock, the Db::open method will fail and
213either return <a href="../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or
214throw a <a href="../api_cxx/deadlock_class.html">DbDeadlockException</a> exception.</p>
215<p>If a Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable
216to grant a lock in the allowed time, the Db::open method will fail and
217either return <a href="../ref/program/errorret.html#DB_LOCK_NOTGRANTED">DB_LOCK_NOTGRANTED</a> or
218throw a <a href="../api_cxx/lockng_class.html">DbLockNotGrantedException</a> exception.</p>
219<hr size=1 noshade>
220<b>Description: Db::get_dbname</b>
221<p>The Db::get_dbname method returns the current filename and database
222name.</p>
223<b>Parameters</b> <br>
224 <b>filenamep</b><ul compact><li>The <b>filenamep</b> parameter references memory into which
225a pointer to the current filename is copied.</ul> 
226 <b>dbnamep</b><ul compact><li>The <b>dbnamep</b> parameter references memory into which
227a pointer to the current database name is copied.</ul> 
228<br>
229<p>The Db::get_dbname method may be called at any time during the life of the
230application.</p>
231<p>The Db::get_dbname method
232either returns a non-zero error value
233or throws an exception that encapsulates a non-zero error value on
234failure, and returns 0 on success.
235</p>
236<hr size=1 noshade>
237<b>Description: <a href="../api_cxx/db_open.html">Db::get_multiple</a></b>
238<p>The <a href="../api_cxx/db_open.html">Db::get_multiple</a> method returns non-zero if the <a href="../api_cxx/db_class.html">Db</a> handle
239references a physical file supporting multiple databases.</p>
240<p>In this case, the <a href="../api_cxx/db_class.html">Db</a> handle is a handle on a database whose key
241values are the names of the databases stored in the physical file and
242whose data values are opaque objects.  No keys or data values may be
243modified or stored using the database handle.</p>
244<p>The <a href="../api_cxx/db_open.html">Db::get_multiple</a> method may not be called before the Db::open method is called.</p>
245<hr size=1 noshade>
246<b>Description: Db::get_open_flags</b>
247<p>The Db::get_open_flags method returns the current open method flags.</p>
248<p>The Db::get_open_flags method may not be called before the Db::open method is called.</p>
249<p>The Db::get_open_flags method
250either returns a non-zero error value
251or throws an exception that encapsulates a non-zero error value on
252failure, and returns 0 on success.
253</p>
254<b>Parameters</b> <br>
255 <b>flagsp</b><ul compact><li>The Db::get_open_flags method returns  the
256current open method flags in <b>flagsp</b>.</ul>
257<br>
258<hr size=1 noshade>
259<b>Description: Db::get_transactional</b>
260<p>The Db::get_transactional method returns non-zero if the <a href="../api_cxx/db_class.html">Db</a>
261handle has been opened in a transactional mode.</p>
262<p>The Db::get_transactional method may be called at any time during the life of the
263application.</p>
264<hr size=1 noshade>
265<br><b>Class</b>
266<a href="../api_cxx/db_class.html">Db</a>
267<br><b>See Also</b>
268<a href="../api_cxx/db_list.html">Databases and Related Methods</a>
269</tt>
270<table width="100%"><tr><td><br></td><td align=right>
271<a href="../api_cxx/api_core.html"><img src="../images/api.gif" alt="API"></a><a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a>
272</td></tr></table>
273<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
274</body>
275</html>
276