Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

EnvExample.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: EnvExample.java,v 12.2 2005/06/16 20:22:47 bostic Exp $
00008  */
00009 
00010 package db;
00011 
00012 import com.sleepycat.db.*;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.OutputStream;
00016 
00017 /*
00018  * An example of a program configuring a database environment.
00019  *
00020  * For comparison purposes, this example uses a similar structure
00021  * as examples/ex_env.c and examples_cxx/EnvExample.cpp.
00022  */
00023 public class EnvExample {
00024     private static final String progname = "EnvExample";
00025     private static final File DATABASE_HOME = new File("/tmp/database");
00026 
00027     private static void runApplication(Environment dbenv)
00028         throws DatabaseException {
00029 
00030         // Do something interesting...
00031         // Your application goes here.
00032     }
00033 
00034     private static void setupEnvironment(File home,
00035                                          String dataDir,
00036                                          OutputStream errs)
00037         throws DatabaseException, FileNotFoundException {
00038 
00039         // Create an environment object and initialize it for error reporting.
00040         EnvironmentConfig config = new EnvironmentConfig();
00041         config.setErrorStream(errs);
00042         config.setErrorPrefix(progname);
00043 
00044         //
00045         // We want to specify the shared memory buffer pool cachesize,
00046         // but everything else is the default.
00047         //
00048         config.setCacheSize(64 * 1024);
00049 
00050         // Databases are in a separate directory.
00051         config.addDataDir(dataDir);
00052 
00053         // Open the environment with full transactional support.
00054         config.setAllowCreate(true);
00055         config.setInitializeCache(true);
00056         config.setTransactional(true);
00057         config.setInitializeLocking(true);
00058 
00059         //
00060         // open is declared to throw a FileNotFoundException, which normally
00061         // shouldn't occur when allowCreate is set.
00062         //
00063         Environment dbenv = new Environment(home, config);
00064 
00065         try {
00066             // Start your application.
00067             runApplication(dbenv);
00068         } finally {
00069             // Close the environment.  Doing this in the finally block ensures
00070             // it is done, even if an error is thrown.
00071             dbenv.close();
00072         }
00073     }
00074 
00075     private static void teardownEnvironment(File home,
00076                                             String dataDir,
00077                                             OutputStream errs)
00078         throws DatabaseException, FileNotFoundException {
00079 
00080         // Remove the shared database regions.
00081         EnvironmentConfig config = new EnvironmentConfig();
00082 
00083         config.setErrorStream(errs);
00084         config.setErrorPrefix(progname);
00085         config.addDataDir(dataDir);
00086         Environment.remove(home, true, config);
00087     }
00088 
00089     public static void main(String[] args) {
00090         //
00091         // All of the shared database files live in /tmp/database,
00092         // but data files live in /database/files.
00093         //
00094         // Using Berkeley DB in C/C++, we need to allocate two elements
00095         // in the array and set config[1] to NULL.  This is not
00096         // necessary in Java.
00097         //
00098         File home = DATABASE_HOME;
00099         String dataDir = "/database/files";
00100 
00101         try {
00102             System.out.println("Setup env");
00103             setupEnvironment(home, dataDir, System.err);
00104 
00105             System.out.println("Teardown env");
00106             teardownEnvironment(home, dataDir, System.err);
00107         } catch (DatabaseException dbe) {
00108             System.err.println(progname + ": environment open: " + dbe.toString());
00109             System.exit (1);
00110         } catch (FileNotFoundException fnfe) {
00111             System.err.println(progname + ": unexpected open environment error  " + fnfe);
00112             System.exit (1);
00113         }
00114     }
00115 
00116 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2