~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/java/src/com/sleepycat/examples/EnvExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * See the file LICENSE for redistribution information.
 
3
 *
 
4
 * Copyright (c) 1997-2001
 
5
 *      Sleepycat Software.  All rights reserved.
 
6
 *
 
7
 * $Id: EnvExample.java,v 11.8 2001/01/25 18:22:53 bostic Exp $
 
8
 */
 
9
 
 
10
package com.sleepycat.examples;
 
11
 
 
12
import com.sleepycat.db.*;
 
13
import java.io.FileNotFoundException;
 
14
import java.io.OutputStream;
 
15
 
 
16
/*
 
17
 * An example of a program using DbEnv to configure its DB
 
18
 * environment.
 
19
 *
 
20
 * For comparison purposes, this example uses a similar structure
 
21
 * as examples/ex_env.c and examples_cxx/EnvExample.cpp.
 
22
 */
 
23
public class EnvExample
 
24
{
 
25
    private static final String progname = "EnvExample";
 
26
    private static final String DATABASE_HOME = "/tmp/database";
 
27
 
 
28
    private static void db_application()
 
29
        throws DbException
 
30
    {
 
31
        // Do something interesting...
 
32
        // Your application goes here.
 
33
    }
 
34
 
 
35
    private static void db_setup(String home, String data_dir,
 
36
                                  OutputStream errs)
 
37
         throws DbException, FileNotFoundException
 
38
    {
 
39
        //
 
40
        // Create an environment object and initialize it for error
 
41
        // reporting.
 
42
        //
 
43
        DbEnv dbenv = new DbEnv(0);
 
44
        dbenv.set_error_stream(errs);
 
45
        dbenv.set_errpfx(progname);
 
46
 
 
47
        //
 
48
        // We want to specify the shared memory buffer pool cachesize,
 
49
        // but everything else is the default.
 
50
        //
 
51
        dbenv.set_cachesize(0, 64 * 1024, 0);
 
52
 
 
53
        // Databases are in a subdirectory.
 
54
        dbenv.set_data_dir(data_dir);
 
55
 
 
56
        // Open the environment with full transactional support.
 
57
        //
 
58
        // open() will throw a DbException if there is an error.
 
59
        //
 
60
        // open is declared to throw a FileNotFoundException, which normally
 
61
        // shouldn't occur with the DB_CREATE option.
 
62
        //
 
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);
 
66
 
 
67
        try {
 
68
 
 
69
            // Start your application.
 
70
            db_application();
 
71
 
 
72
        }
 
73
        finally {
 
74
 
 
75
            // Close the environment.  Doing this in the
 
76
            // finally block ensures it is done, even if
 
77
            // an error is thrown.
 
78
            //
 
79
            dbenv.close(0);
 
80
        }
 
81
    }
 
82
 
 
83
    private static void db_teardown(String home, String data_dir,
 
84
                                    OutputStream errs)
 
85
         throws DbException, FileNotFoundException
 
86
    {
 
87
        // Remove the shared database regions.
 
88
 
 
89
        DbEnv dbenv = new DbEnv(0);
 
90
 
 
91
        dbenv.set_error_stream(errs);
 
92
        dbenv.set_errpfx(progname);
 
93
        dbenv.set_data_dir(data_dir);
 
94
        dbenv.remove(home, 0);
 
95
    }
 
96
 
 
97
    public static void main(String[] args)
 
98
    {
 
99
        //
 
100
        // All of the shared database files live in /tmp/database,
 
101
        // but data files live in /database.
 
102
        //
 
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.
 
106
        //
 
107
        String home = DATABASE_HOME;
 
108
        String config = "/database/files";
 
109
 
 
110
        try {
 
111
            System.out.println("Setup env");
 
112
            db_setup(home, config, System.err);
 
113
 
 
114
            System.out.println("Teardown env");
 
115
            db_teardown(home, config, System.err);
 
116
        }
 
117
        catch (DbException dbe) {
 
118
            System.err.println(progname + ": environment open: " + dbe.toString());
 
119
            System.exit (1);
 
120
        }
 
121
        catch (FileNotFoundException fnfe) {
 
122
            System.err.println(progname +
 
123
                               ": unexpected open environment error  " + fnfe);
 
124
            System.exit (1);
 
125
        }
 
126
    }
 
127
 
 
128
}