1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1997-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package db; 10 11import com.sleepycat.db.*; 12import java.io.File; 13import java.io.FileNotFoundException; 14import java.io.OutputStream; 15 16/* 17 * An example of a program configuring a database environment. 18 * 19 * For comparison purposes, this example uses a similar structure 20 * as examples/ex_env.c and examples_cxx/EnvExample.cpp. 21 */ 22public class EnvExample { 23 private static final String progname = "EnvExample"; 24 25 private static void runApplication(Environment dbenv) 26 throws DatabaseException, FileNotFoundException { 27 28 // Open a database in the environment to verify the data_dir 29 // has been set correctly. 30 DatabaseConfig dbconfig = new DatabaseConfig(); 31 32 // The database is DB_BTREE. 33 dbconfig.setAllowCreate(true); 34 dbconfig.setMode(0644); 35 dbconfig.setType(DatabaseType.BTREE); 36 Database db=dbenv.openDatabase(null, 37 "jEnvExample_db1.db", null, dbconfig); 38 39 // Close the database. 40 db.close(); 41 } 42 43 private static void setupEnvironment(File home, 44 File dataDir, 45 OutputStream errs) 46 throws DatabaseException, FileNotFoundException { 47 48 // Create an environment object and initialize it for error reporting. 49 EnvironmentConfig config = new EnvironmentConfig(); 50 config.setErrorStream(errs); 51 config.setErrorPrefix(progname); 52 53 // 54 // We want to specify the shared memory buffer pool cachesize, 55 // but everything else is the default. 56 // 57 config.setCacheSize(64 * 1024); 58 59 // Databases are in a separate directory. 60 config.addDataDir(dataDir); 61 62 // Open the environment with full transactional support. 63 config.setAllowCreate(true); 64 config.setInitializeCache(true); 65 config.setTransactional(true); 66 config.setInitializeLocking(true); 67 68 // 69 // open is declared to throw a FileNotFoundException, which normally 70 // shouldn't occur when allowCreate is set. 71 // 72 Environment dbenv = new Environment(home, config); 73 74 try { 75 // Start your application. 76 runApplication(dbenv); 77 } finally { 78 // Close the environment. Doing this in the finally block ensures 79 // it is done, even if an error is thrown. 80 dbenv.close(); 81 } 82 } 83 84 private static void teardownEnvironment(File home, 85 File dataDir, 86 OutputStream errs) 87 throws DatabaseException, FileNotFoundException { 88 89 // Remove the shared database regions. 90 EnvironmentConfig config = new EnvironmentConfig(); 91 92 config.setErrorStream(errs); 93 config.setErrorPrefix(progname); 94 config.addDataDir(dataDir); 95 Environment.remove(home, true, config); 96 } 97 98 private static void usage() { 99 System.err.println("usage: java db.EnvExample [-h home] [-d data_dir]"); 100 System.exit(1); 101 } 102 103 public static void main(String[] argv) { 104 // 105 // All of the shared database files live in home, 106 // but data files live in dataDir. 107 // 108 // Using Berkeley DB in C/C++, we need to allocate two elements 109 // in the array and set config[1] to NULL. This is not 110 // necessary in Java. 111 // 112 File home = new File("TESTDIR"); 113 File dataDir = new File("data"); 114 115 for (int i = 0; i < argv.length; ++i) { 116 if (argv[i].equals("-h")) { 117 if (++i >= argv.length) 118 usage(); 119 home = new File(argv[i]); 120 } else if (argv[i].equals("-d")) { 121 if (++i >= argv.length) 122 usage(); 123 dataDir = new File(argv[i]); 124 } else if (argv[i].equals("-u")) { 125 usage(); 126 } 127 } 128 129 try { 130 System.out.println("Setup env"); 131 setupEnvironment(home, dataDir, System.err); 132 133 System.out.println("Teardown env"); 134 teardownEnvironment(home, dataDir, System.err); 135 } catch (DatabaseException dbe) { 136 System.err.println(progname + ": environment open: " + dbe.toString()); 137 System.exit (1); 138 } catch (FileNotFoundException fnfe) { 139 System.err.println(progname + ": unexpected open environment error " + fnfe); 140 System.exit (1); 141 } 142 } 143 144} 145