2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
package com.sleepycat.examples;
12
import com.sleepycat.db.*;
13
import java.io.FileNotFoundException;
14
import java.io.OutputStream;
17
* An example of a program using DbEnv to configure its DB
20
* For comparison purposes, this example uses a similar structure
21
* as examples/ex_env.c and examples_cxx/EnvExample.cpp.
23
public class EnvExample
25
private static final String progname = "EnvExample";
26
private static final String DATABASE_HOME = "/tmp/database";
28
private static void db_application()
31
// Do something interesting...
32
// Your application goes here.
35
private static void db_setup(String home, String data_dir,
37
throws DbException, FileNotFoundException
40
// Create an environment object and initialize it for error
43
DbEnv dbenv = new DbEnv(0);
44
dbenv.set_error_stream(errs);
45
dbenv.set_errpfx(progname);
48
// We want to specify the shared memory buffer pool cachesize,
49
// but everything else is the default.
51
dbenv.set_cachesize(0, 64 * 1024, 0);
53
// Databases are in a subdirectory.
54
dbenv.set_data_dir(data_dir);
56
// Open the environment with full transactional support.
58
// open() will throw a DbException if there is an error.
60
// open is declared to throw a FileNotFoundException, which normally
61
// shouldn't occur with the DB_CREATE option.
63
dbenv.open(DATABASE_HOME,
64
Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_LOG |
65
Db.DB_INIT_MPOOL | Db.DB_INIT_TXN, 0);
69
// Start your application.
75
// Close the environment. Doing this in the
76
// finally block ensures it is done, even if
77
// an error is thrown.
83
private static void db_teardown(String home, String data_dir,
85
throws DbException, FileNotFoundException
87
// Remove the shared database regions.
89
DbEnv dbenv = new DbEnv(0);
91
dbenv.set_error_stream(errs);
92
dbenv.set_errpfx(progname);
93
dbenv.set_data_dir(data_dir);
94
dbenv.remove(home, 0);
97
public static void main(String[] args)
100
// All of the shared database files live in /tmp/database,
101
// but data files live in /database.
103
// Using Berkeley DB in C/C++, we need to allocate two elements
104
// in the array and set config[1] to NULL. This is not
105
// necessary in Java.
107
String home = DATABASE_HOME;
108
String config = "/database/files";
111
System.out.println("Setup env");
112
db_setup(home, config, System.err);
114
System.out.println("Teardown env");
115
db_teardown(home, config, System.err);
117
catch (DbException dbe) {
118
System.err.println(progname + ": environment open: " + dbe.toString());
121
catch (FileNotFoundException fnfe) {
122
System.err.println(progname +
123
": unexpected open environment error " + fnfe);